当前位置: 首页 > news >正文

electron 渲染进程按钮创建新window,报BrowserWindow is not a constructor错误;

在 Electron 中,有主进程和渲染进程
主进程:在Node.js环境中运行—意味着能够使用require模块并使用所有Node.js API
渲染进程:每个electron应用都会为每个打开的BrowserWindow(与每个网页嵌入)生成一个单独的渲染器进程。
windows 中展示的界面通过渲染器进程渲染且一个应用可以有多个渲染进程
因此,一个浏览器窗口的所有的用户界面和应用功能,都应该是在网页开发上使用相同的工具和规范来写(如html,css,js)
因此这也意味着渲染器无权直接访问require或其他Node.js API.

BrowserWindow 是一个只能在主进程中使用的类,而不能直接在渲染进程中创建。因此,当你在渲染进程中尝试直接使用 BrowserWindow 创建新窗口时,会出现 BrowserWindow is not a constructor 的错误。

以下是几种解决方法:

方法 1:通过 IPC 通信

在渲染进程中通过 IPC 向主进程发送消息,主进程接收到消息后创建新窗口。这是推荐的方式,因为它符合 Electron 的设计。

主进程代码(main.js
const { app, BrowserWindow, ipcMain } = require('electron');
let mainWindow = null;const createWindow = () => {mainWindow = new BrowserWindow({webPreferences: {nodeIntegration: true,contextIsolation: false,},});mainWindow.loadFile('index.html');
};app.on('ready', createWindow);ipcMain.on('create-new-window', () => {const newWindow = new BrowserWindow({width: 400,height: 300,webPreferences: {nodeIntegration: true,contextIsolation: false,},});newWindow.loadFile('new-window.html');
});
渲染进程代码(index.html
<button id="newWindow">创建窗口</button>
<script>const { ipcRenderer } = require('electron');document.getElementById('newWindow').addEventListener('click', () => {ipcRenderer.send('create-new-window');});
</script>

方法 2:使用 @electron/remote 模块

虽然 Electron 官方不推荐使用 @electron/remote,但可以通过它在渲染进程中直接创建 BrowserWindow

主进程代码
const { app, BrowserWindow } = require('electron');
require('@electron/remote/main').initialize();let mainWindow = null;const createWindow = () => {mainWindow = new BrowserWindow({webPreferences: {enableRemoteModule: true,contextIsolation: false,},});require('@electron/remote/main').enable(mainWindow.webContents);mainWindow.loadFile('index.html');
};app.on('ready', createWindow);
渲染进程代码
const { BrowserWindow } = require('@electron/remote');
document.getElementById('newWindow').addEventListener('click', () => {const newWindow = new BrowserWindow({width: 400,height: 300,});newWindow.loadFile('new-window.html');
});

方法 3:使用 window.open

如果需要从渲染进程直接打开新窗口,可以使用 window.open 方法,并通过 webContents.setWindowOpenHandler 在主进程中自定义窗口的创建。

主进程代码
const { app, BrowserWindow } = require('electron');
let mainWindow = null;const createWindow = () => {mainWindow = new BrowserWindow({webPreferences: {nodeIntegration: true,contextIsolation: false,},});mainWindow.loadFile('index.html');mainWindow.webContents.setWindowOpenHandler((details) => {const newWindow = new BrowserWindow({width: 400,height: 300,webPreferences: {nodeIntegration: true,contextIsolation: false,},});newWindow.loadURL(details.url);return { action: 'deny' }; // 阻止默认行为});
};app.on('ready', createWindow);
渲染进程代码
<button id="newWindow">创建窗口</button>
<script>document.getElementById('newWindow').addEventListener('click', () => {window.open('new-window.html', '_blank');});
</script>

总结

  • 推荐使用方法 1,通过 IPC 通信,符合 Electron 的设计,安全性更高。
  • 如果需要快速实现,可以使用方法 2,但需要注意 @electron/remote 的安全性和未来兼容性。
  • 方法 3 更适合需要从渲染进程直接打开窗口的场景,但需要在主进程中进行严格控制。
http://www.xdnf.cn/news/14437.html

相关文章:

  • 嵌入式设备网络的动态ID分配机制实现
  • 极狐GitLab 用户 API 速率限制如何设置?
  • CenterTrack
  • DNS解析失败怎么解决?
  • 【Spring Boot 源码学习】深入 ConfigurableEnvironment 的初始化过程
  • 论文阅读笔记——Mixtral of Experts
  • 中级社会工作者考试精选练习题
  • 深度学习-全连接神经网络-1
  • C++代码优化
  • 梯度下降代码
  • fatdds:传输层SHM和DATA-SHARING的区别
  • 数据结构|基数排序及八个排序总结
  • Python爬虫入门
  • 使用veaury,在vue项目中运行react组件
  • 汉诺塔专题:P1760 通天之汉诺塔 题解 + Problem D: 汉诺塔 题解
  • AI写程序: 多线程网络扫描网段ip工具
  • STM32使用rand()生成随机数并显示波形
  • 【最后203篇系列】028 FastAPI的后台任务处理
  • JVM之经典垃圾回收器
  • C++数据结构与二叉树详解
  • Kubernetes》》k8s》》Namespace
  • ProfibusDP转ModbusRTU网关,流量计接入新方案!
  • React 中如何获取 DOM:用 useRef 操作非受控组件
  • 珈和科技:无人机技术赋能智慧农业,精准施肥与病虫害监控全面升级
  • Perf学习
  • 使用最新threejs复刻经典贪吃蛇游戏的3D版,附完整源码
  • Spring Boot配置文件优先级全解析:如何优雅覆盖默认配置?
  • 盲超分-双循环对比学习退化网络(蒸馏)
  • Cursor 生成java测试用例
  • k8s低版本1.15安装prometheus+grafana进行Spring boot数据采集