简体   繁体   English

Electron Forge - 在渲染器文件中使用 ipcRenderer

[英]Electron Forge - Use ipcRenderer in the renderer file

Behavior行为

I am develop a Electron Forge project.我正在开发一个电子锻造项目。 When I'm trying to use electron.ipcRenderer , it will cause an error, the app cannot be started.当我尝试使用electron.ipcRenderer ,它会导致错误,应用程序无法启动。 It shows some informations, but it's useless:它显示了一些信息,但没有用:

An unhandled rejection has occurred inside Forge:
[Error: EISDIR: illegal operation on a directory, read] {
  errno: -4068,
  code: 'EISDIR',
  syscall: 'read'
}

Electron Forge was terminated. Location:
{}
error Command failed with exit code 1.

I had created a project with Electron before with Electron forge, is works fine with ipcRenderer我之前用 Electron ipcRenderer创建了一个项目,在ipcRenderer工作正常

Only import in typescript cause the error, require in HTML works fine只有在打字稿中导入会导致错误,HTML 中的 require 工作正常

More Information更多信息

electron.ipcRenderer and electron.remote both cause problem, but import without using will not cause the error. electron.ipcRendererelectron.remote都会导致问题,但不使用 import 不会导致错误。

Window create code:窗口创建代码:

const mainWindow = new BrowserWindow({
    height: 540,
    width: 960,
    resizable: false,
    frame: false,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
      contextIsolation: false,
      preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
    },
  });

The resolution in Electron Forge - Can't use ipcRenderer in the renderer file is not work, which is access ipcRenderer in preload.ts Electron Forge 中的分辨率- Can't use ipcRenderer in the renderer file is not work,这是在 preload.ts 中访问 ipcRenderer

Envrionment环境

Important package versions重要的包版本

    "@electron-forge/cli": "^6.0.0-beta.57",
    "typescript": "^4.0.2"
    "electron": "^13.1.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "@material-ui/core": "^4.11.4",
    "@material-ui/icons": "^4.11.2",

OS操作系统

Windows 10 x64, CoreCountrySpecific (aka. 家庭中文版 or Chinese Family Edition), Version 2009, 21H1 (19043.1083), Windows Feature Experience Pack 120.2212.3530.0. Windows 10 x64, CoreCountrySpecific (aka.家庭中文版或中文家庭版), Version 2009, 21H1 (19043.1083), Windows Feature Experience Pack 120.2212.3530.0。

CPU: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz 2.59 GHz CPU:Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz 2.59 GHz

GPU: NVIDIA GeForce GTX 1660 Ti GPU:NVIDIA GeForce GTX 1660 Ti

Possible Reasons可能的原因

  • Webpack can't complies electron, it should be complies directly to require('electron') , instead inject electron code into the file webpack 不能兼容电子,应该直接兼容require('electron') ,而是将电子代码注入文件中
    • If in this case, how to configure webpack to make it doesn't complies electron into a file?如果在这种情况下,如何配置 webpack 使其不将电子编译为文件?
  • Electron forge question电子锻造问题
  • This electron version and electron forge version cannot be used together此电子版和电子锻造版不能一起使用
    • If in this case, which version should I provide?如果在这种情况下,我应该提供哪个版本?
  • React.js problem React.js 问题
    • If in this case, should I remove react or use react 16?如果在这种情况下,我应该删除 react 还是使用 react 16?

Use preload.ts , import module from electron, and declare it on window.使用preload.ts ,从电子导入模块,并在窗口上声明它。

For example:例如:

preload.ts

// noinspection ES6UnusedImports
import { ipcRenderer, remote } from 'electron'; // eslint-disable-line @typescript-eslint/no-unused-vars

/* eslint-disable @typescript-eslint/no-explicit-any */
window.ipcRenderer = ipcRenderer;
window.remote = remote;
/* eslint-enable */

index.d.ts : index.d.ts

interface Window {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  ipcRenderer: any;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  remote: any;
}

Use:采用:

window.remote.dialog.showSaveDialogSync({});
window.ipcRenderer.send('close');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM