Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么windows.dispatchEvent()无法在Angular 9上的app.component.ts上运行_Javascript_Angular_Typescript_Electron - Fatal编程技术网

Javascript 为什么windows.dispatchEvent()无法在Angular 9上的app.component.ts上运行

Javascript 为什么windows.dispatchEvent()无法在Angular 9上的app.component.ts上运行,javascript,angular,typescript,electron,Javascript,Angular,Typescript,Electron,在我的ngOnInit中的app.component.ts中,我使用了这个名为log的方法来发送分派事件。当我将angular应用程序加载到Electron webview标记中时,这将在预加载脚本中进行监听。出于某种原因,我的角度前端的每一个其他部分都可以执行此调度,但当我尝试在app.component.ts中执行此操作时,它永远不会工作 public log(level: LogLevel, message: string) { const data = {

在我的ngOnInit中的app.component.ts中,我使用了这个名为log的方法来发送分派事件。当我将angular应用程序加载到Electron webview标记中时,这将在预加载脚本中进行监听。出于某种原因,我的角度前端的每一个其他部分都可以执行此调度,但当我尝试在app.component.ts中执行此操作时,它永远不会工作

public log(level: LogLevel, message: string) {
        const data = {
            message: '============== frontend : ' + message,
            level
        };
        window.dispatchEvent(new MessageEvent('logMessage', {
            data,
            bubbles: false,
            cancelable: false
        }));
    }

从这部分代码中,我不明白为什么它不起作用。重要的是检查另一侧,即您正在收听此
logMessage
事件的位置。从angular主组件触发Ngonit事件可能会导致并发问题,您需要确保在触发此事件之前完成侦听器注册