3

我有一个电子应用程序,它加载应用程序网络版本的 URL。但是,我无法通过单击 X 按钮关闭应用程序,并且我无权访问 webapp。我试过这个:

let count = BrowserWindow.getAllWindows().length; 
///returns 1, so there is only 1 window)
                        
window.onbeforeunload=function(e){
    e.returnValue=undefined;
    return undefined;
}
window.close();
app.quit();

什么都没发生。

app.exit(0)有效,但我不想使用它。有没有办法可以用window.closeand关闭应用程序app.quit

编辑:问题是那些全局的 beforeunload 事件。如果我在 devtools 中单击删除,我可以关闭该应用程序。 在此处输入图像描述

这也let names= window.eventNames();返回一个没有任何beforeunload事件的数组

4

2 回答 2

1

您需要保存BrowserWindow对象引用。然后侦听“关闭”事件并取消引用它:

const { app, BrowserWindow } = require('electron');

let win; // = new BrowserWindow();

win.on('closed', () => {
  win = null
})

取消引用后,一切都很简单,只需在app上监听 'window-all-closed' 事件:

app.on('window-all-closed', () => {
  app.quit()
})
于 2019-07-10T18:42:20.200 回答
1

In the file where you created the BrowserWindow, you can attach an event to the 'close' handler for the window. For Electron, this is usually main.js.

const {app, BrowserWindow} = require('electron');
let mainWindow = null;
app.on('ready', () => {
    mainWindow = new BrowserWindow({
       //options here
    });
    mainWindow.loadURL([url here]);

    //attach event handler here
    mainWindow.on("close", () => {
       mainWindow = null;
       app.quit();
    });
});

For good measure, in the case that you may have more than one window, place this in your main.js file.

app.on('window-all-closed', () => {
     if(process.platform !== "darwin")
         app.quit();
});
于 2019-07-10T18:35:02.450 回答