【发布时间】:2019-10-19 11:12:25
【问题描述】:
我正在尝试访问 childView 实例,但它一直说 childView 未定义。
这是我的 childViews 代码:
@ViewChild(CreateQuestionnaireComponent,{ read: true, static: false }) private childQuestionnaireDialog:CreateQuestionnaireComponent;
@ViewChild(ProjectNavbarComponent,{ read: true, static: false }) private childProjectNavBar:ProjectNavbarComponent;
@ViewChild(QuestionnaireNodeComponent,{ read: true, static: false }) private childQuestionnaireNode:QuestionnaireNodeComponent;
....
onCreateTerminal() {
this.childQuestionnaireDialog.generateQuestionnaireDropDownList();
this.childQuestionnaireDialog.resetFields();
this._hideQuestionnaireDialog = false;
this._modalTitle = 'New Terminal';
this._placeHolderText = 'Terminal Questionnaire Title';
this._terminal = true;
}
...
上面写着:this.childQuestionnaireDialog is undefined"。
它正在使用 Angular 7。
根据我的新知识,@viewChild 带有一个名为 static 的标志。如果我们将该标志设置为 true,则父组件会在其自己的创建过程中尝试获取对 childView 的引用。换句话说,我们可以在父组件的 onInit() 方法中拥有一个 childView 的实例。基本上是一次性访问,因为我们无法在任何其他方法中访问。
标志设置为 false,基本上是 ivy 渲染器中的新方式。
就我而言,这两个选项都不起作用。
【问题讨论】:
-
stackblitz 上的演示会很棒。
-
你的html代码在哪里?
-
尝试删除读取选项
-
我尝试删除读取标志,但没有帮助。
标签: javascript angular web angular8