【问题标题】:Electron + angularjs IPC mechanismElectron + angularjs IPC 机制
【发布时间】:2025-12-08 19:20:09
【问题描述】:

我正在尝试在 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');

但由于 Angular 在浏览器 (webkit) 中运行,我基本上不能使用 require 来获取 ipcRenderer。

如何解决这个问题。

【问题讨论】:

标签: javascript angularjs node.js ipc electron


【解决方案1】:

可以在电子中使用require - 它使用此功能扩展了 webkit API。基本上整个 NPM 都在你的掌控之中。好吧,显然有些事情不会起作用,但require 会。

【讨论】:

  • 简单来说,Electron 扩展了 webkit 本身以添加诸如require 等功能?
  • 是的,添加了这个和更多的 API。
  • 包括 ipcRenderer 功能 :D
【解决方案2】:

应该可以,但是你需要添加nodeIntegration

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

在 BrowserWindow 构造函数中

【讨论】: