如何从电子渲染过程中打开新的模态浏览器窗口 [英] How can I open a new modal browser windows from a render process in electron

查看:50
本文介绍了如何从电子渲染过程中打开新的模态浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在渲染过程中使用 new BrowserWindow 语句时,会引发以下异常: TypeError:BrowserWindow不是构造函数.

如何从渲染过程中创建新的模式化BrowserWindow?

解决方案

您不能在渲染器进程中使用 BrowserWindow .仅在主要过程中:

请参见

When using the new BrowserWindow statement in the render process the following exception is thrown: TypeError: BrowserWindow is not a constructor.

How can I create a new modal BrowserWindow from a render process?

解决方案

You cannot use BrowserWindow in the renderer process. Only in the main process:

See https://www.electronjs.org/docs/api/browser-window#browserwindow

The two processes must communicate via a channel:

// main process
const {app, BrowserWindow, ipcMain} = require('electron')

app.whenReady().then(function () {
  const mainWindow = new BrowserWindow({ webPreferences: { nodeIntegration: true } });
  mainWindow.loadFile('index.html');
});

ipcMain.on('go!', (ev, url) => {
//         ^^^^^
//         A channel called "go!"
  new BrowserWindow().loadURL(url);
});

// renderer process
const {ipcRenderer} = require('electron');

document.querySelector('button').addEventListener('click', function () {
  ipcRenderer.send('go!', 'https://www.discogs.com/artist/7760-Japanese-Telecom');
});

<html>
  <body>
    <button>detroit techno</button>
    <script>require('./renderer.js')</script>
  </body>
</html>

When you click on the button "detroit techno" (as you should!):

  1. The renderer process sends a message with a url
  2. The main process picks up the message and open the url in a new window

这篇关于如何从电子渲染过程中打开新的模态浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆