繁体   English   中英

在 Angular 中,如何在与当前组件不同的组件中运行方法

[英]In Angular, how do I run a method in a different component than the current component

如何在 Angular 中的组件 X 中运行组件 Y 中的方法。

X 组件中的方法

  onClear() {
    this.editor.clear();
    // environment.templateId=0;
    this.openScheduler();
  }

像这样的Y分量


 X.onclear();

使用服务在组件之间进行交互或使用共享方法。

如果它是父子组件关系,您可以这样做。

@ViewChild('componentName', { static: false })
  component: SomeComponent;

在模板中:

<component #componentName></component>

比你可以:

  onClear() {
    this.editor.clear();
    // environment.templateId=0;
    this.component.doSomehting();
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM