出于这个原因,我们开始使用 NWJS,也因为它支持chrome.serial
. 最近我将项目转换为电子项目有几个原因:
没错,NWJS 没有主/渲染过程的复杂性,但我发现很少有理由必须处理 IPC。
许多 API 仅在主进程中可用,但可以通过远程API 访问。因此,例如要从我使用的渲染过程中访问主要内容: process.argv
{process} = require('electron').remote
process.argv ...
在我的 index.js 中,我不得不做一些 IPC 的事情,但是电子有库来简化这个:
// ensure we only have a single instance, but pass args to renderer to open any files passed by explorer
const shouldQuit = app.makeSingleInstance((argv, workingDir) => {
win.webContents.send('open-instance', argv);
})
然后在我的渲染器代码中,我有以下内容:
ipcRenderer.on('open-instance', (event, arg) => {
this.restoreAndFocus();
// TODO - handle another instance opening a file from explorer
});