一、问题
高版本 require(electron).remote.BrowserWindow 报错 undefined,是因为从V14开始移除了remote,要使用的话需用通过npm引入
1、通过 npm 方式引入 remote
npm install --save @electron/remote2、在主进程 mian.js 中添加引用代码
require("@electron/remote/main").initialize();
require("@electron/remote/main").enable(win.webContents);
3、在渲染进程中引用
// 1、获取ID为btn的选择器
const btn = this.document.querySelector('#btn');
// 2、创建窗口引用
const { BrowserWindow } = require("@electron/remote");
// 3、打开窗口
window.onload = function () {
btn.onclick = () => {
newWin = new BrowserWindow({ width: 300, height: 200 })
newWin.loadFile('test1.html')
newWin.on('close', () => { newWin = null })
}
}