Electron + angularjs IPC机制

Electron + angularjs IPC mechanism

我正在尝试在 Electron 中使用 AngularJs。我感到困惑的是,the electron docs here 建议使用类似的东西:

// In renderer process (web page).
const ipcRenderer = require('electron').ipcRenderer;
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // prints "pong"

ipcRenderer.on('asynchronous-reply', function(event, arg) {
    console.log(arg); // prints "pong"
});
ipcRenderer.send('asynchronous-message', 'ping');

But since Angular is running inside the browser(webkit) I cannot essentially use require to get the ipcRenderer.

如何克服这个问题。

可以 在 electron 中使用 require - 它使用此功能扩展了 webkit API。基本上整个 NPM 都由你支配。好吧,有些事情显然行不通,但是 require 会。

它应该可以工作,但您需要添加 nodeIntegration

mainWindow = new BrowserWindow({
        width: 1024,
        height: 632,
        webPreferences: {
            nodeIntegration: true
        }
    })

在 BrowserWindow 构造函数中