@ViewChild(AlertComponent) alert: AlertComponent;
ngAfterViewInit(){
debugger;
var msg: Message ={ message: 'Saved successfully!', status: true };
console.log(this.alert.show(msg))
}
success(){
debugger;
//Saved successfully show the message
var msg: Message ={ message: 'Saved successfully!', status: true };
this.alert.show(msg);
}
这是我的代码。我无法致电警报服务。它显示了未定义的viewchild
警报组件:
export class AlertComponent {
message: Message = null;
constructor() {}
show(msg: Message) {
debugger;
this.message = msg;
setTimeout(() => {
this.message = null;
}, 2000)
}
}
答案 0 :(得分:-2)
您应该在父组件的模板中使用Alert Component
。无论如何,您应该尝试在输入/输出或服务等组件之间进行另一种类型的通信。
更多信息:https://angular.io/guide/component-interaction