簡體   English   中英

如何在子組件的ngOnInit之后調用父組件中的方法

[英]how to call method in parent component after ngOnInit of child component

在父組件中,我想等待子組件的 ngOnInit。 我嘗試使用 setTimeout 它可以工作,但我不喜歡使用它。

setTimeout(() => {
}, 1500); ```

不知道您的目的,但您可以使用 Output() 和 EventEmitter 來完成。 (此代碼未經測試)

家長

childInit() {
  console.log('Child is initialized');
}
<child (initializedEmitter)="childInit()"></child>

孩子

@Output() initializedEmitter = new EventEmitter<any>();


ngAfterViewInit() { // Maybe you can try with ngOnInit()
  this.initializedEmitter.emit();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM