【问题标题】:Acess to node modules in Electron 5访问 Electron 5 中的节点模块
【发布时间】:2019-10-17 07:43:11
【问题描述】:

由于 Electron 的最新版本 5 出于安全原因,nodeIntegration 默认为 false,因此推荐的访问节点模块的方式是什么?没有nodeIntegration有没有办法和主进程通信?

【问题讨论】:

    标签: electron


    【解决方案1】:

    使用预加载脚本,您可以与主进程通信,只需将 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 对象与主进程/或另一个渲染器进程进行通信。

    【讨论】:

    • 请提供一个示例,或者至少提供一个指向 Electron 文档的链接,说明如何做到这一点。这将使这个答案对其他人更有用......
    • 我在这里找到了一个预加载脚本的小例子:electronjs.org/docs/tutorial/…,这是一个关于该主题的很好的链接。但是有更多关于它和关于 ipc 的例子会很好。
    • 我在答案中添加了更多详细信息,请参阅。
    • 另见我的回答,它提供了更多信息和示例,这里:stackoverflow.com/questions/52236641/…
    猜你喜欢
    • 2016-02-24
    • 2017-06-28
    • 2018-07-22
    • 1970-01-01
    • 2014-07-20
    • 2021-04-27
    • 2016-01-15
    • 2019-02-27
    • 2020-01-28
    相关资源
    最近更新 更多