我已经升级到Angular 8.2.3,并将所有ViewChild调用都转换为包括static参数。现在,如果将组件包装在* ngIf中,则在访问组件时会遇到问题。
以前这可行,我能够调用组件的方法:
html:
<div *ngIf="someBooleanResult">
...
<MyComponent #mycomponent1></MyComponent>
...
</div>
ts:
...
@ViewChild('mycomponent1') mycomponent1: MyComponent;
...
ngAfterViewInit () {
if(someBooleanResult) {
this.mycomponent1.someMethod();
}
}
现在,在Angular 8中,此@ViewChild调用始终未定义mycomponent1(我也尝试过static:true,但这也不起作用):
@ViewChild('mycomponent1', { static: false}) mycomponent1: MyComponent;
关于新的ViewChild设计,我缺少什么?
答案 0 :(得分:0)
我今天在工作中遇到了同样的问题,来自GünterZöchbauer的回答通过以下链接为我解决了这个问题:
还请注意,由于您是在Angular检测到要显示的数据更改期间修改值的,因此这可能会导致“检查后表达式已更改”错误使用该解决方案后我也遇到了。
您还可以在此处详细了解: https://blog.angular-university.io/angular-debugging