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

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器进程中创建子窗口时如何修复 BrowserWindow 不是构造函数错误)
本文介绍了在 Electron 渲染器进程中创建子窗口时如何修复 BrowserWindow 不是构造函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I'm using electron to build an application that includes two windows. I'm trying to open a second window from inside renderer process doing something like:

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

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

我收到一个错误提示

BrowserWindow 不是构造函数.

BrowserWindow is not a constructor.

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

My other option is to use window.open, but that is not ideal since that returns BrowserWindowProxy object, which has limited functionality.

推荐答案

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

I found that all I needed to do was to use the remote module. Electron doesn't allow to directly create a browser window from the renderer process, because it (BrowserWindow) requires ipc module to communicate with the main process. Electron documentation says:

在 Electron 中,与 GUI 相关的模块(如对话框、菜单等)仅在主进程中可用,在渲染器进程中不可用.为了在渲染器进程中使用它们,需要 ipc 模块将进程间消息发送到主进程.

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 模块正确设置了与主进程的进程间通信,以下修改后的代码对我有用:

So, new electron.BrowserWindow() doesn't work. However, using remote module correctly sets up inter-process communicating with the main process and the following modified code works for me:

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/

A more complete explanation of remote module is here: https://electron.atom.io/docs/api/remote/

这篇关于在 Electron 渲染器进程中创建子窗口时如何修复 BrowserWindow 不是构造函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)