簡體   English   中英

關於 ipc 事件的 Electron-Angular 打開客戶端對話框

[英]Electron-Angular open client-side dialog on ipc event

我有一個后端通知,它會彈出一個客戶端角度材質對話框組件。 有時,但並非總是如此,對話框不會完全實例化。 組件的構造函數被調用,但生命周期中的其他任何東西都不會被調用,直到對話框關閉。 有時它工作得很好並且ngOnInit也被調用。

具有 ipc 訂閱的組件如下所示(我也嘗試過setTimeout和使用Observable ,但都沒有成功):

api.receive('hotkey', (event, arg) => {
  this.onHotkey(shortcut as Shortcut);
});
api.send('setHotkeyListener');

熱鍵實現調用與執行this.dialog.open(...)業務的按鈕相同的方法。

api 是通過preload.jscontextBridge實現的,如下所示:

const {
  contextBridge,
  ipcRenderer
} = require("electron");

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld(
  "api", {
    sendSync: (channel, data) => {
      return ipcRenderer.sendSync(channel, data);
    },
    send: (channel, data) => {
      ipcRenderer.send(channel, data);
    },
    receive: (channel, func) => {
      ipcRenderer.on(channel, (...args) => func(...args));
    }
  }
);

我認為問題是一些缺失的上下文,即 angular 沒有從 ipcRenderer 代碼路徑中獲取; 你知道我錯過了什么嗎?

這很難找到,但很容易解決。 希望我的問題也有助於為其他人指明正確的方向。 我需要使用NgZone讓 angular 知道它需要檢測變化。

import { NgZone, OnInit } from '@angular/core';

// ...

declare const api: any;

export class AppComponent implements OnInit {
  constructor(private ngZone: NgZone) {}
  ngOnInit() {
    api.receive('hotkey', (event, arg) => {
      this.ngZone.run(() => this.onHotkey(arg as Shortcut));
    });
    api.send('setHotkeyListener');
  }
  // ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM