【发布时间】:2020-05-30 11:53:57
【问题描述】:
升级到 Angular 9 后,基本上我所有的 @ViewChild 引用都不再初始化了。
不管怎样,
<app-menu-editor #menuEditor>
</app-menu-editor>
<div #cardBody>
<!-- ... -->
</div>
@ViewChild('menuEditor', {read: MenuEditorComponent}) menuEditor: MenuEditorComponent;
@ViewChild('cardBody', {read: ElementRef}) cardBody: ElementRef;
我不断收到异常告诉我,例如menuEditor 是 undefined。
知道为什么这不再起作用了吗?
【问题讨论】:
-
很可能是因为您在
ngOnInit而不是ngAfterViewInit中访问它们
标签: angular