@ViewChild升级到Angular 9后无法正常工作 [英] @ViewChild not working after upgrading to Angular 9

查看:237
本文介绍了@ViewChild升级到Angular 9后无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到Angular 9后,基本上我所有的@ViewChild引用都不再初始化.

After upgrading to Angular 9, basically all my @ViewChild references are not initialized anymore.

无论如何,

<app-menu-editor #menuEditor>
</app-menu-editor>

<div #cardBody>
  <!-- ... -->
</div>

@ViewChild('menuEditor', {read: MenuEditorComponent}) menuEditor: MenuEditorComponent;

@ViewChild('cardBody', {read: ElementRef}) cardBody: ElementRef;

我不断收到一些异常消息,例如menuEditorundefined.

I keep getting exceptions telling me that e.g. menuEditor is undefined.

您知道为什么它不再起作用了吗?

Any idea why this is not working anymore?

推荐答案

尝试

@ViewChild('menuEditor', {static: true, read: MenuEditorComponent}) menuEditor: MenuEditorComponent;

@ViewChild('cardBody', {static: true, read: ElementRef}) cardBody: ElementRef;

就像PierreDuc所说的那样,也许您在ngOnInit中是指它们.

Like PierreDuc said, maybe you're referring to them in the ngOnInit.

这篇关于@ViewChild升级到Angular 9后无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆