【问题标题】:Calling method of a child component if parent has two children of the same component如果父组件有同一个组件的两个子组件,则调用子组件的方法
【发布时间】:2017-09-17 19:07:11
【问题描述】:

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-view-child

所以根据“官方”文档,子组件的调用方法可以在父类的内部使用:

@ViewChild(CountdownTimerComponent)
private timerComponent: CountdownTimerComponent;

做事

timerComponent.methodName()

那么如果父组件使用了两个CountDownTimerComponent并且只想调用timerComponentNumber1.methodName()呢?

假设开发者想要调用 ParentClass 内部的方法,而不是模板中的方法

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您需要改用@ViewChildren

    @ViewChildren(CountdownTimerComponent)
    private timerComponents: QueryList<CountdownTimerComponent>;
    

    您可以遍历timerComponents 并调用您想要的方法;或者做这样的事情:

    this.timerComponents.toArray()[0].someMethod();
    

    这是对the documentation的引用。

    【讨论】:

      猜你喜欢
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 2021-06-10
      • 2018-07-04
      • 2016-12-08
      • 2020-06-15
      相关资源
      最近更新 更多