微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Angular中访问子包对象

如何解决如何在Angular中访问子包对象

我在 app.component.html

中使用了一个程序包
    <app-my-test2 *ngIf="data" #MyTest2Component></app-my-test2>

这是 app-my-test2 软件包。 (使用ngPackager)

并在 app.component.ts

中使用
@ViewChild('MyTest2Component',{static: true}) myTest2Component: MyTest2Component;

在此之前,请将 app-my-test2 转换为软件包,所有程序都可以正常工作,并且我访问 app-my-test2 “此”,但是 将 app-my-test2 转换为软件包后,“ this”返回undefind

解决方法

问题是我的* ngIf。 * ngIf指令正在杀死我的控件组件,因此我无法引用它。

前面提到的问题是ngIf,它导致视图未定义。答案是使用ViewChildren而不是ViewChild。我遇到了类似的问题,即在所有参考数据都加载完毕之前,我不希望显示网格。

 @ViewChildren("MyTest2Component") public myTest2Component: QueryList<MyTest2Component>


public ngAfterViewInit() {

    this.myTest2Component.changes.subscribe((comps: QueryList <MyTest2Component>) =>
    {
        console.log(comps.first) ;
    });

}

document

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。