简体   繁体   English

如何访问 Angular 中的子包对象

[英]How to get access to child package object in Angular

I used a package in app.component.html我在app.component.html 中使用了一个包

    <app-my-test2 *ngIf="data" #MyTest2Component></app-my-test2>

here's app-my-test2 package.这是app-my-test2包。 (with ngPackager) (使用 ngPackager)

and used in app.component.tsapp.component.ts 中使用

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

befor, convert app-my-test2 to package, all work correctly and i access app-my-test2 "this", but after convert app-my-test2 to package, "this" returned undefind之前,将app-my-test2转换为包,一切正常,我访问了app-my-test2 “this”,但是在将app-my-test2转换为包后,“this”返回 undefind

The problem was my *ngIf.问题是我的 *ngIf。 The *ngIf directive was killing my controls component so I couldn't reference it. *ngIf 指令正在杀死我的控件组件,因此我无法引用它。

The issue as previously mentioned is the ngIf which is causing the view to be undefined.前面提到的问题是导致视图未定义的 ngIf。 The answer is to use ViewChildren instead of ViewChild.答案是使用 ViewChildren 而不是 ViewChild。 I had similar issue where I didn't want a grid to be shown until all the reference data had been loaded.我有类似的问题,在加载所有参考数据之前我不希望显示网格。

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


public ngAfterViewInit() {

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

}

document文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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