簡體   English   中英

Electron 應用程序:無法加載預加載腳本

[英]Electron App : Unable to load preload script

每次啟動我的 electron 應用程序時都會收到此錯誤。

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'),
    },
  });

預加載.js

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electron', {
  notificationAPI: {
    sendNotification(message) {
      ipcRenderer.send('notify', message);
    },
  },
  filesAPI: {
    sendEmails(content) {
      ipcRenderer.send('save-delete', content);
    },
  },
});

由於錯誤提示需要啟用contextIsolation以允許您使用contextBridge API

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      contextIsolation: true,
      preload: path.join(__dirname, 'preload.js'),
    },
  });

注意:從 Electron 12.0 開始默認啟用contextIsolation

我有類似的錯誤。 就我而言,原因是代碼中有一個導入周期,ig fileA 導入 fileB 導入 fileC 導入 fileA。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM