![](/img/trans.png)
[英]ViewChild returns undefined while returning from child component to parent component
[英]parent component accesses one of its child via ViewChild but it is always undefined
我的模板中有一個組件,其中另一個組件是我自己的:
<div (click)="onEditStarted()">
...
<my-child *ngIf="someCondition"></my-child>
...
</div>
在父母中,我有:
@ViewChild(MyChildComponent) child: MyChildComponent;
但是在我需要child
,它是undefined
。
OnEditStarted() {
...
setTimeout(() => this.child.someMethod(), 0);
...
}
我還嘗試否定someCondition
並將其放入[hidden]
屬性而不是*ngIf
但它仍未undefined
。 有任何想法嗎?
我認為問題出在* ngIf指令中。 它正在殺死my-child組件,因此無法引用它。 您可以嘗試使用ViewChildren
代替ViewChild
或嘗試使用ng-template
包裝ngIf
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.