[英]Getting undefined nativeElement when using @viewchid with *ngIf condition
Getting undefined nativeElement when using @viewchid with *ngIf condition使用带有 *ngIf 条件的 @viewchid 时获取未定义的 nativeElement
Error错误
Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'nativeElement') Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'nativeElement')
@ViewChild('myElement', { static: false }) myElement: ElementRef<HTMLElement>;
不要在构造函数中调用它, ngAfterViewInit
是第一个定义它的钩子。
If your `*ngIf condition is dynamic you should probably use a setter.如果您的 `*ngIf 条件是动态的,您可能应该使用 setter。
@ViewChild('myElement') set setMyElement(myElement: ElementRef) {
if (myElement) {
// Here you can access content.nativeElement etc.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.