Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如果父组件具有同一组件的两个子组件,则调用子组件的方法_Angular_Typescript - Fatal编程技术网

Angular 如果父组件具有同一组件的两个子组件,则调用子组件的方法

Angular 如果父组件具有同一组件的两个子组件,则调用子组件的方法,angular,typescript,Angular,Typescript,因此,根据“官方”文档,子组件的调用方法可以在父类内部使用: @ViewChild(CountdownTimerComponent) private timerComponent: CountdownTimerComponent; 做 timerComponent.methodName() 那么,如果父组件使用两个CountDownTimerComponent,并且只想调用timerComponentNumber1.methodName() 假设开发人员希望在父类内部而不是从模板调用方法,则

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

@ViewChild(CountdownTimerComponent)
private timerComponent: CountdownTimerComponent;

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


假设开发人员希望在父类内部而不是从模板调用方法,则需要使用
@ViewChildren

@ViewChildren(CountdownTimerComponent)
private timerComponents: QueryList<CountdownTimerComponent>;
这里是参考

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