电子+ angularjs IPC机制

时间:2016-02-19 20:36:48

标签: javascript angularjs node.js ipc electron

我正在尝试在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。

如何克服这个问题。

2 个答案:

答案 0 :(得分:1)

可以在电子中使用require - 它使用此功能扩展了webkit API。基本上整个NPM都可供您使用。好吧,有些事情显然不会奏效,但require会。

答案 1 :(得分:0)

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

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

在BrowserWindow构造函数中