【问题标题】:Can't use `require` in Electron with TypeScript Create React App nor preload script不能在带有 TypeScript Create React App 和预加载脚本的 Electron 中使用`require`
【发布时间】:2021-03-14 06:23:08
【问题描述】:

我正在尝试在 (Create)React(app) 代码中导入 Electron ipcRenderer,但遇到了障碍:

  1. 预加载脚本未执行:

new BrowserWindow({
      titleBarStyle: "customButtonsOnHover",
      resizable: false,
      width: config.width,
      height: config.height,
      webPreferences: {
        contextIsolation: false,
        nodeIntegration: true,
        preload: path.join(__dirname, "..", "preload.js"),
      },
    })
  1. 我尝试将脚本添加到 index.html 中
  <script>
      console.log(require);
  </script>

导致:

require 未定义

似乎是由于 webpack 路径无法正常工作?将电子 ipcRenderer 导入 React 代码应该使用哪种解决方案?

【问题讨论】:

标签: electron


【解决方案1】:

还需要为视图启用节点:

 new BrowserView({
    webPreferences: {
      nodeIntegration: true,
    },
  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-07
    • 2018-09-21
    • 2021-06-18
    • 2021-01-23
    • 2021-11-25
    • 2019-10-21
    • 2017-04-23
    • 2020-08-05
    相关资源
    最近更新 更多