[英]In Angular, how do I run a method in a different component than the current component
How do I run a method in component Y from component X in Angular.如何在 Angular 中的组件 X 中运行组件 Y 中的方法。
method in X component X 组件中的方法
onClear() {
this.editor.clear();
// environment.templateId=0;
this.openScheduler();
}
like so Y component像这样的Y分量
X.onclear();
Use a service to interact between components or to use shared methods.使用服务在组件之间进行交互或使用共享方法。
If it's parent-child component relation you can do this like this.如果它是父子组件关系,您可以这样做。
@ViewChild('componentName', { static: false })
component: SomeComponent;
And in template:在模板中:
<component #componentName></component>
Than you can just:比你可以:
onClear() {
this.editor.clear();
// environment.templateId=0;
this.component.doSomehting();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.