无法访问角形父级组件中的@ViewChild属性

问题描述 投票:1回答:2

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'

我在做什么错?

angular components undefined lifecycle viewchild
2个回答
0
投票

尝试这样:

。html

<app-child #ChildComponent></app-child>

。ts

@ViewChild("ChildComponent", {static: true}) child:ChildComponent;

0
投票

在如下所示的视图中指定组件名称和类型

@ViewChild(HelloComponent, {static: false}) hello: HelloComponent;

请参见示例stackblitz

© www.soinside.com 2019 - 2024. All rights reserved.