首页 文章

输出事件emmiter在Angular 5中引发错误

提问于
浏览
0

我有一个奇怪的问题,我在Angular 5.2.11中有一个带有父子组件的项目 . 一切正常,直到我在子组件中添加@Output装饰器以发出对父组件的响应 . 这是孩子TS:

questionCorrectAnswer:number;

nextQuestion(){
   this.nextQuestionEvent.emit({});
}

这是孩子的HTML

<a (click)="nextQuestion()" class="btn btn-primary">Go to next question</a>

在父母的HTML中我有这个:

<app-question-displayer (nextQuestionEvent)="nextQuestion($event)"></app-question-displayer>

最后父母的TS文件如下:

setQuestion(idx:number){
   this.currentQuestion=this.exam.questions[idx];
   this.questionDisplay.setupQuestion(this.currentQuestion);
}

nextQuestion(event){
   this.currentQuestionIndex++;
   this.setQuestion(  this.currentQuestionIndex);
}

一旦页面加载,它会将我重定向到主页,在Chrome的控制台中,我看到此错误:

enter image description here

正如你所看到的那样,这不是一个非常具有描述性的错误,因为我用Google搜索了它,但发现了其他与我没有太大关系的错误 .

有一些奇怪的事情,如果我在app-question-displayer标签中删除事件监听器(nextQuestionEvent)=“nextQuestion($ event)”,该应用程序不会崩溃 .

你觉得怎么样?

提前致谢!

编辑:

这是输出定义:

@Output() nextQuestionEvent:EventEmitter<any>;

1 回答

  • 1

    您尚未实例化您的EventEmitter .

    使用以下代码执行此操作:

    @Output() nextQuestionEvent:EventEmitter<any> = new EventEmitter();
    

相关问题