【发布时间】:2019-10-17 07:43:11
【问题描述】:
由于 Electron 的最新版本 5 出于安全原因,nodeIntegration 默认为 false,因此推荐的访问节点模块的方式是什么?没有nodeIntegration有没有办法和主进程通信?
【问题讨论】:
标签: electron
由于 Electron 的最新版本 5 出于安全原因,nodeIntegration 默认为 false,因此推荐的访问节点模块的方式是什么?没有nodeIntegration有没有办法和主进程通信?
【问题讨论】:
标签: electron
使用预加载脚本,您可以与主进程通信,只需将 ipcRenderer 对象导入窗口。
为此,您必须在 browserWindow webPreferences 中指定预加载脚本的绝对路径。
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
preload : path.join(__dirname , '/preload_script.js')
}
})
并在 preload_script.js 中注入 ipcRenderer 对象
window.ipcRenderer = require('electron').ipcRenderer;
您可以使用 html 脚本中的 window.ipcRenderer 对象与主进程/或另一个渲染器进程进行通信。
【讨论】: