这个错误通常是由于@ViewChild查询尝试读取未定义属性(例如'next')而引起的。
为了解决这个问题,需要确保在使用ViewChild之前,目标组件已经加载完毕。可以使用ngAfterViewInit或ngOnInit生命周期挂钩来确保加载完成。此外,还需要确保查询中的选择器正确地选择要查询的元素。
以下是解决该问题的示例代码:
@Component({
selector: 'my-component',
template:
})
export class MyComponent implements AfterViewInit {
@ViewChild('child')
private childComp: ChildComponent;
ngAfterViewInit() { console.log(this.childComp.next); } }