簡體   English   中英

Electron JS - 無法解構 'require(...).remote' 的屬性 'BrowserWindow',因為它未定義

[英]Electron JS - Cannot destructure property 'BrowserWindow' of 'require(…).remote' as it is undefined

這是在渲染器進程中:


const {BrowserWindow} = require('electron').remote

const path = require('path')
const url = require('url')

const newWindowButton = document.getElementById('new-window-btn');
newWindowButton.addEventListener('click',(e)=>{
    let win3 = new BrowserWindow();
    win3.loadURL(url.format({
        pathname: path.join(__dirname,'index3.html'),
        protocol: "file",
        slashes: true
    }))

})

我無法在渲染器進程中打開一個新窗口,出現以下錯誤。

**未捕獲的類型錯誤:無法按原樣解構 'require(...).remote' 的屬性 'BrowserWindow'

undefined.**
    at Object.<anonymous> (D:\ElectronTute\helloWorld\index1.js:4)
    at Object.<anonymous> (D:\ElectronTute\helloWorld\index1.js:21)
    at Module._compile (internal/modules/cjs/loader.js:1145)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js`enter code here`:1166)
    at Module.load (internal/modules/cjs/loader.js:981)
    at Module._load (internal/modules/cjs/loader.js:881)
    at Function.Module._load (electron/js2c/asar.js:769)
    at Module.require (internal/modules/cjs/loader.js:1023)
    at require (internal/modules/cjs/helpers.js:77)
    at index1.html:13
 mainWindow = new BrowserWindow({
    width: 1280,
    height: 960,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
     },
  });

我相信您正在使用新版本的 Electron。 從 v9 版本開始,我們不允許在渲染remote上使用remote ,除非將enableRemoteModule設置為 true。

另外,為了使用require()在渲染器上加載node_moduels ,我們還需要啟用nodeIntegration 作為 require 是節點 API 之一。

https://github.com/electron/electron/issues/21408

暫無
暫無

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

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