javascript - 在 Electron 渲染器进程中创建子窗口时如何修复 BrowserWindow 不是构造函数错误

标签 javascript electron

我正在使用 electron 构建一个包含两个窗口的应用程序。我正在尝试从渲染器进程内部打开第二个窗口,执行如下操作:

const electron = require('electron');
const BrowserWindow = electron.BrowserWindow;

const childWindow = new BrowserWindow({
   width: 800,
   height: 600
});

我收到一条错误提示

BrowserWindow is not a constructor.

我的另一个选择是使用 window.open,但这并不理想,因为它会返回功能有限的 BrowserWindowProxy 对象。

最佳答案

我发现我需要做的就是使用 remote 模块。 Electron 不允许直接从渲染器进程创建浏览器窗口,因为它(BrowserWindow)需要 ipc 模块来与主进程通信。 Electron 文档说:

In Electron, GUI-related modules (such as dialog, menu etc.) are only available in the main process, not in the renderer process. In order to use them from the renderer process, the ipc module is necessary to send inter-process messages to the main process.

因此,new electron.BrowserWindow() 不起作用。 但是,使用 remote 模块可以正确设置与主进程的进程间通信,以下修改后的代码对我有用:

const electron = require('electron');
const BrowserWindow = electron.remote.BrowserWindow;

const childWindow = new BrowserWindow({
   width: 800,
   height: 600
});

remote 模块更完整的解释在这里:https://electron.atom.io/docs/api/remote/

关于javascript - 在 Electron 渲染器进程中创建子窗口时如何修复 BrowserWindow 不是构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45639628/

相关文章:

javascript - 我不能在 Electron 中使用fs.readFile函数

api - VueJS + Electron,API出现问题

javascript - 在物化设计中设置自动播放轮播的间隔

javascript - 如何在特定脚本上调用参数为 `findRow(s)` 的函数?

javascript - 使用 Angular 表达式处理 JSON 字符串返回的 bool 数据

node.js - Bootstrap Fileinput无法隐藏进度条(nodejs/Electron)

windows - 带 Electron 链接的msi安装程序

javascript - 以编程方式绑定(bind)事件 (SyntheticEvent)

javascript - 使用 JS 从 <a> 链接获取 ID 或 VALUE 或 NAME

javascript - Electron - 打开文件夹对话框