reactjs - Electron 9 remote.getCurrentWindow()。close()不起作用

标签 reactjs electron

我有一个两个窗口的应用程序,第一个窗口是这样创建的:

mainWindow = new BrowserWindow({
    width: 1024, // width of the window
    height: 768, // height of the window
    show: false, // don't show until window is ready
    frame: false,
    webPreferences: {
        nodeIntegration: true,
        enableRemoteModule: true
    }
})
在该用户可以提示打开第二个窗口之后,将调用以下功能
import { remote } from "electron";

let dev = require('electron-is-dev');
const path = require('path')
const url = require('url')

let win

export default function createPopupWindow(currentLanguage) {

    win = new remote.BrowserWindow({
        width: 500,
        height: 700,
        frame: false,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true
        }
    });

    win.webContents.on('did-finish-load', () => {
        win.webContents.send('message', { language: currentLanguage });
    });


    let indexPath

    // Determine the correct index.html file
    // (created by webpack) to load in dev and production
    if (dev && process.argv.indexOf('--noDevServer') === -1) {
        indexPath = url.format({
            protocol: 'http:',
            host: 'localhost:8080',
            pathname: '/src/editWindow/index.html',
            slashes: true
        })
    } else {
        // todo
        indexPath = url.format({
            protocol: 'file:',
            pathname: path.join(__dirname, 'index.jsx'),
            slashes: true
        })
    }

    // Load the index.html
    win.loadURL(indexPath)

    win.once('ready-to-show', () => {
        win.show()
    })
}
问题是从 Electron 8升级到9后,我无法调用以下命令关闭第二个窗口。我已经发现关闭事件被调用,但是窗口没有关闭。
import { ipcRenderer, remote } from 'electron';
...
remote.getCurrentWindow().close();
提前致谢!
干杯

最佳答案

我在 Electron 版9.1.2中也遇到了同样的问题,最后我找到了解决它的方法。关键是您不能将窗口直接保留在mainProcess上。
之前在main.js中,我有一个变量来存储窗口对象,现在我将其更改为存储window.id,并定义一个辅助函数来通过windowId获取窗口。
前:

let mainWindow;

const createWindow = () => {
  mainWindow = new BrowserWindow({
    width: 300,
    height: 440,
    frame: false,
  });
}

app.on("ready", createWindow);
后:
let mainWindowId = 0

const createWindow = () => {
  let mainWindow = new BrowserWindow({
    width: 300,
    height: 440,
    frame: false,
  });
  mainWindowId = mainWindow.id
}

app.on("ready", createWindow);

const getMainWindow = () => {
  return BrowserWindow.getAllWindows().filter(
    (window) => window.id === mainWindowId
  )[0];
};

关于reactjs - Electron 9 remote.getCurrentWindow()。close()不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63142365/

相关文章:

javascript - 文本节点不能作为 <table> 的子节点出现 [ReactJS];警告 : Each child in a list should have a unique "key" prop

css - iOS 15 Safari CSS 中新的安全区域插入方法是什么?

javascript - 无需浏览器 GUI 的文本转语音

javascript - 需要使用 Vue.js 或 Javascript 在 Electron 中播放 WAV 文件,Chrome 不支持 WAV

node.js - 使用 craco 的 React App 不会在 docker-compose 中启动

javascript - 对象的循环属性

javascript - 如何在 Electron.Atom\WebPack 应用程序中使用 FS 模块?

electron - 在Mac和Windows上均无法为Electronic 5.0.10安装node-libcurl

javascript - 如何为Range添加onKeyDown事件?

javascript - Electron - 应用内购买 - 如何使用 PHP 验证苹果收据?