[英]Electron Forge - Use ipcRenderer in the renderer file
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 工作正常
electron.ipcRenderer
and electron.remote
both cause problem, but import without using will not cause the error. electron.ipcRenderer
和electron.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
"@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",
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
require('electron')
, instead inject electron code into the file require('electron')
,而是将电子代码注入文件中
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.