【发布时间】:2021-06-18 17:01:09
【问题描述】:
每次启动电子应用程序时都会收到此错误。
electron/js2c/renderer_init.js:91 Unable to load preload script: C:\Users\Desktop\Projects\Electron-Apps\Electron\src\preload.js
(anonymous) @ electron/js2c/renderer_init.js:91
electron/js2c/renderer_init.js:91 Error: contextBridge API can only be used when contextIsolation is enabled
我的 preload.js 与我的 index.js 位于同一目录中,所以我在调试时有点不知所措。不知道为什么它不会加载我的 preload.js 文件。在我使用 eslint 将默认格式化程序设置为更漂亮之前,一切正常。
索引.js
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
});
Preload.js
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electron', {
notificationAPI: {
sendNotification(message) {
ipcRenderer.send('notify', message);
},
},
filesAPI: {
sendEmails(content) {
ipcRenderer.send('save-delete', content);
},
},
});
【问题讨论】:
标签: javascript electron