angular - Angular 9:如何从 templateRef 访问子组件实例
问题描述
在 Angular 8 中,我能够从 templateRef 访问子组件,如下所示:
<ng-template #templateName>
<child-component></child-component>
</ng-template>
@ViewChild('templateName') template;
const childComponent = template._projectedViews[0].nodes[1].instance;
但是在将应用程序迁移到版本 9 之后,一切都发生了变化。现在,我在模板中没有投影视图。
现在我将以下属性视为 templateRef 的一部分
templateRef
-> elementRef
-> _declarationContainer
-> _declarationView
我现在如何访问 Angular 9 中的子组件实例?
解决方案
要访问您的子组件,请尝试如下:
@ViewChild(ChildComponent) template: ChildComponent;
ChildComponent
您的子组件的类名在哪里。
现在template
您将能够访问子组件的属性。
推荐阅读
- java - 如何集成 Gradle、IntelliJ 和 SpotBugs?
- scala - Scala 中的偏函数
- quarkus - 我需要向 sam.jvm.yaml 添加 CORS 参数,有没有办法在 Quarkus 中做到这一点?
- react-native - 尝试使用 xcodebuild 创建的 .app 文件运行排毒测试时出错
- django - Django——查看 MultiValueDictKeyError
- c# - C#将项目添加到对象列表字段
- mysql - 优化此 sql 查询的替代方法是什么?
- reactjs - 在用户登录到 React 路由器 dom 中的帐户后,您通常如何重定向用户?
- devexpress - 如何在 GeneXus 中集成 DevExpress
- sql - 将两个表结果组合成 JSON - SQL server