我有一个Ionic 2应用程序,它的ParentComponent调用ChildComponent @ViewChild方法来启动多个ChildComponents。在 View 中使用不同的参数实例化了一个ChildComponents两次,如下所示:
<ChildComponent [startFrom]="0" [limitTo]="1"></ChildComponent>
<ChildComponent [startFrom]="1" [limitTo]="1"></ChildComponent>
脱机/在线设备状态更改后,我调用ChildComponent的方法来更新其返回的项目列表。
@ViewChild(ChildComponent) childComponent: ChildComponent;
ngOnInit(): void {
this.networkService.connectSubscription(() => {
this.childComponent.getItems();
});
}
这里的问题是
this.childComponent
仅获得两者的第一个ChildComponent实例。有没有办法遍历同一个@ViewChild组件的多个实例,以便我可以执行
this.childComponent1.getItems()
和this.childComponent2.getItems()
之类的操作?谢谢你的帮助。
最佳答案
@ViewChildren(ChildComponent) childComponents: QueryList<ChildComponent>;
ngAfterViewInit(): void {
this.networkService.connectSubscription(() => {
this.childComponents.toArray();
});
}