How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process

早过忘川 提交于 2020-08-06 07:32:12

问题


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
});

I'm getting an error saying

BrowserWindow is not a constructor.

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


回答1:


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:

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.

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
});

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



来源:https://stackoverflow.com/questions/45639628/how-to-fix-browserwindow-is-not-a-constructor-error-when-creating-child-window-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!