Angular-如何触发位于不同文件中的Custom组件上的Click事件

问题描述 投票:0回答:1

我有这个自定义组件:

export class MainLayoutGenerated implements AfterViewInit, OnInit, OnDestroy {
  // Components
  @ViewChild('sidebar-toggle0') sidebarToggle0: SidebarToggleComponent;

位于root/MainLayout/MainLayoutGenerated.ts

并且我想从此处触发该组件Click event

root/MyComp/MainComp.ts

我可以通过此文件以不同的方式访问我的自定义组件,例如:

constructor(private mainLayout: MainLayoutGenerated) {
  }

 ngAfterViewInit() {

this.mainLayout.sidebarToggle0;
  }

或类似这样:

export class MainComp {
  @ViewChild(MainLayoutGenerated) sidebarToggle: MainLayoutGenerated;

constructor() {}

ngAfterViewInit() {
    this.sidebarToggle.sidebarToggle0;
  }

但是我发现了有关如何触发clickEvent的所有解决方案,例如:

this.COMPONENT.nativeElement.click()

但是我的自定义组件不是NativeElement,这如何工作?

我需要在ngAfterViewInit()组件的MainComp中触发点击事件

angular custom-component viewchild
1个回答
0
投票

我不会触发点击事件。只需调用sidebarToggle方法即可。

export class MainComp implements AfterViewInit {
  @ViewChild(MainLayoutGenerated) sidebarToggle: MainLayoutGenerated;

  ngAfterViewInit() {
    this.sidebarToggle.sidebarToggle();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.