简体   繁体   English

@ViewChild 返回未定义的 Angular 8

[英]@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 文件中,我尝试了以下操作,但都返回未定义:

  1. @ViewChild(myComponent, { static: true }) child: myComponent; @ViewChild(myComponent, { static: true }) child: myComponent;

  2. @ViewChild('myComp', { static: false }) child: myComponent; @ViewChild('myComp', { static: false }) child: myComponent;

  3. @ViewChild('myComp', {static:true}) child: ElementRef; @ViewChild('myComp', {static:true}) child: ElementRef;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM