我有这个自定义组件:
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
中触发点击事件
我不会触发点击事件。只需调用sidebarToggle方法即可。
export class MainComp implements AfterViewInit {
@ViewChild(MainLayoutGenerated) sidebarToggle: MainLayoutGenerated;
ngAfterViewInit() {
this.sidebarToggle.sidebarToggle();
}
}