嗨,当我单击“发送消息”按钮代码与路由器配合使用时,效果很好,但是当我在goback链接上单击该代码时,该代码在控制台上发送错误,并且停止。
您可以通过以下方式签入我的编辑链接的stackbliz:https://stackblitz.com/edit/angular2-communicating-between-components-qjhrpu?file=app%2Ffeedback%2Ffeedback.component.html
答案 0 :(得分:1)
您必须影响您的订阅,否则您的this.subscription
未定义。
ngOnInit() {
this.subscription = this.messageService.getNewUserInfo().subscribe(info => {
this.message = info;
console.log("here",info);
})
}
答案 1 :(得分:1)
首先,您必须为this.subscription
订阅分配值。
ngOnInit() {
this.subscription = this.messageService.getNewUserInfo().subscribe(info => {
this.message = info;
console.log("here",info);
})
}
然后还要检查ngOnDestroy
上的值。
ngOnDestroy() {
// unsubscribe to ensure no memory leaks
if(this.subscription) {
this.subscription.unsubscribe();
}
}