当我在 Windows 10 上构建npm start
应用程序时,它无法正常运行,而在 macOS 上运行良好。
包.json
{
"name": "SimpleTimer",
"version": "1.0.0",
"productName": "SimpleTimer",
"copyright": "",
"description": "",
"main": "main.js",
"scripts": {
"start": "node_modules/.bin/electron .",
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"electron": "^10.1.2"
}
}
我收到以下错误消息。
TypeError: ipcMain.handle is not a function
这适用于:
main.js
ipcMain.handle("ipc-timer-start", () => {
if ( isWorking === true ) {
return true
}
else {
StartTimer();
isWorking = true;
}
return true;
});
这个函数是从ipcRenderer.invoke()
写入的 ipc 通信的接收者preload.js
。这就是下面源码invoke()
中api中的方法。TimerStart
preload.js
const { contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld(
"api", {
TimerStart: () =>
ipcRenderer.invoke("ipc-timer-start") /*** HERE ***/
.then(result => result)
.catch(err => console.log(err)),
TimerStop: () => ipcRenderer.send("ipc-timer-stop"),
TimerReset: () => ipcRenderer.send("ipc-timer-reset"),
DisplayTimer: (channel, listener) => {
ipcRenderer.on("ipc-display-timer", (event, arg) => listener(arg));
}
}
);
当然,preload.js
是在创建时指定的BrowserWindow
。
main.js
mainWindow = new BrowserWindow({
title: config.name,
width: 1024,
height: 640,
minWidth: 1024,
minHeight: 640,
webPreferences: {
worldSafeExecuteJavaScript: true,
nodeIntegration: false,
contextIsolation: true,
preload: __dirname + '/preload.js' /*** HERE ***/
}
});
跳过此错误消息对话框后,我使用 DevTools 进行了检查,看起来好像preload.js
没有从Unable to load preload script:
.
鉴于这些陈述,似乎preload.js
没有正确包含在构建中,我该怎么办?
再一次,它在 macOS 上运行良好,但由于这些问题,它在 Windows 10 上无法正常运行。