关闭子窗口时主窗口引用变为空

时间:2018-07-10 15:51:30

标签: javascript node.js electron

在我的main.js中,我正常创建主窗口(let mainWin = null,在启动时初始化),并且当按下某个按钮时,我添加了子窗口。

let createNoteWin = null;

function createNoteEditWindow() {
    createNoteWin = new BrowserWindow({ parent:mainWin, modal: true, width: 350, height: 500 , frame: false, show: false});
    createNoteWin.on('close', function() {mainWin = null});
    createNoteWin.loadFile('src/addNote.html');
    createNoteWin.isResizable = false;
    //createNoteWin.webContents.openDevTools();
}

ipcMain.on('open-noteedit-window', function newNoteWindowIPC(event, arg) {  
    createNoteEditWindow();
    createNoteWin.show();
})

但是,当我关闭子窗口时,一旦按下另一个按钮,对我的主窗口的引用就会突然变成null

ipcMain.on('close-noteedit-window', function closeNoteeditWindowIPC(event, arg) {
    mainWin.webContents //works fine
    createNoteWin.close(createNoteWin);
    mainWin.webContents; //throws an error since mainWin is null now, for some reason
})

有人知道为什么/如何发生吗?

1 个答案:

答案 0 :(得分:0)

在第5行,您完全可以完成抱怨的事情:

    createNoteWin.on('close', function() {mainWin = null});

这会将mainWin设置为null(“对我的主窗口的引用突然变为空”)。也许您是这个意思吗?

    createNoteWin.on('close', function() {createNoteWin = null});