【问题标题】:Calling a sibling method throws 'Cannot read property 'X' of undefined'调用同级方法会引发“无法读取未定义的属性 'X'”
【发布时间】:2018-12-19 15:03:49
【问题描述】:

我对 Angular 和 Angular 中的事件非常陌生。

我的父模板:

    <child1 (myEvent)="child2.testMethod()"></child1>
    <child2 #child2 *ngIf="show"></child2>

我收到“无法读取未定义的属性 'testMothod'”错误,但是当 child2 组件中的 *ngIf 被删除时,会调用 'testMethod' 方法。

演示:https://stackblitz.com/edit/angular-1c8hfj

【问题讨论】:

    标签: angular angular6 angular-event-emitter


    【解决方案1】:

    这是一个已知的角度问题https://github.com/angular/angular/issues/6179

    @ViewChild('child2') child2; 添加到AppComponent 即可解决。

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 2022-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2018-04-06
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多