[英]@ViewChild returns undefined Angular 8
For some reason my @ViewChild in my Angular 8 App does not work "undefined".出于某种原因,我的 Angular 8 应用程序中的 @ViewChild 无法“未定义”工作。 I have defined it like this in html file:
我在 html 文件中是这样定义的:
<div class="container">
<div class="row h-100">
<kendo-splitter orientation="vertical">
<kendo-splitter-pane>
<kendo-splitter>
<kendo-splitter-pane>
...
</kendo-splitter-pane>
<kendo-splitter-pane>
<div>
<myComponent #myComp ....> </<myComponent> // ViewChild id here
</div>
</kendo-splitter-pane>
</kendo-splitter>
</kendo-splitter-pane>
</kendo-splitter>
in ts file I tried the following and all return undefined:在 ts 文件中,我尝试了以下操作,但都返回未定义:
@ViewChild(myComponent, { static: true }) child: myComponent; @ViewChild(myComponent, { static: true }) child: myComponent;
@ViewChild('myComp', { static: false }) child: myComponent; @ViewChild('myComp', { static: false }) child: myComponent;
and I used ngAfterViewInit but still get undefined.我使用了 ngAfterViewInit 但仍然未定义。 Is it because myCompnent is wrapped by many html elements?
是不是因为myCompnent被很多html元素包裹了?
Try尝试
@ViewChild('myComp', { static: false }) child: myComponent;
Or if you are using或者,如果您正在使用
@ViewChild(myComponent, { static: true }) child: myComponent;
Make sure that myComponent
is the component class name and not the selector (just to be clear because I cannot confirm it from the example code)确保
myComponent
是组件类名而不是选择器(只是为了清楚,因为我无法从示例代码中确认)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.