[英]@ViewChild component undefined when trying to call async variable
我使用了@ViewChildren 组件:
@ViewChildren("MyTest2Component") public myTest2Component: QueryList<MyTest2Component>
public ngAfterViewInit() {
this.myTest2Component.changes.subscribe((comps: QueryList <MyTest2Component>) =>
{
console.log(comps.first.data); // data is async, here is undefind
});
}
但是当试图调用异步变量数据时,返回undefin
我能做些什么来解决这个问题?
在您的子组件中,您可以定义一个事件来在数据发生变化时通知父组件,如下所示。
@Component({
selector: 'parent-component',
template: '<child-component (dataChanged)="onChildDataChanged($event)"></child-component>',
styles: ['']
})
export class ParentComponent {
onChildDataChanged(newData) {
// tweak with new data
}
}
@Component({
selector: 'child-component',
template: '<div></div>',
styles: ['']
})
export class ChildComponent implements OnInit {
@Output() dataChanged: EventEmitter<any> = new EventEmitter<any>();
ngOnInit(): void {
someAsyncOperation.subscribe(data => {
this.dataChanged.emit(data);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.