我有一个这样的嵌套子组件:
<app-main>
<child-component />
</app-main>
我的 appMain
组件需要在子组件上调用一个方法。
如何调用子组件的方法?
最佳答案
您可以使用以下方法获取对元素的引用
@ViewChild('childComponent') child;
哪里childComponent
是一个模板变量 <some-elem #childComponent
>` 或
@ViewChild(ComponentType) child;
哪里ComponentType
是组件或指令的类型,然后在 ngAfterViewInit
中或事件处理程序调用 child.someFunc()
.
ngAfterViewInit() {
console.log(this.child);
}
关于javascript - 调用子组件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31013461/