child.component.ts
currentPage = 1;
parent.component.ts
export class ParentComponent implements OnInit, AfterViewInit {
@ViewChild(ChildComponent, {static: false}) child;
ngAfterViewInit() {
console.log(this.child.currentPage); // throws error
}
parent.component.ts
<app-child></app-child>
我试图设置一个超时时间,但是它也不起作用。
错误TypeError:无法读取未定义的属性'currentPage'
我在做什么错?
尝试这样:
。html
<app-child #ChildComponent></app-child>
。ts
@ViewChild("ChildComponent", {static: true}) child:ChildComponent;
在如下所示的视图中指定组件名称和类型
@ViewChild(HelloComponent, {static: false}) hello: HelloComponent;
请参见示例stackblitz