【发布时间】:2017-03-07 16:43:27
【问题描述】:
我有一个带有 NodeJS 和 Express 的 Electron 应用程序。我在一个文件(app.js)中有主进程代码,在另一个文件(router.js)中有路由。
主文件创建主窗口:
mainWindow = new BrowserWindow({width: 1280, height: 800, icon: iconPath});
每当您单击应用程序中的 pdf 文档链接时,路由文件都会创建一个新窗口:
router.get('/docs/:module/:type/:file', function(req, res) {
openPDF(req.params.module,req.params.type,req.params.file);
res.end();
});
// open pdf's in a new window
let newWindow;
const openPDF = function(module,filetype,filename) {
let file = 'file:///' + __dirname + '/app/docs/' + module + '/' + filetype + '/' + filename;
let newWindow = new BrowserWindow({
width: 800,
height: 600,
icon: iconPath,
webPreferences: {
nodeIntegration: false
}
});
newWindow.setMenu(null);
// newWindow.webContents.openDevTools();
const param = qs.stringify({file: file});
newWindow.loadURL('file://' + __dirname + '/app/pdfjs/web/viewer.html?' + param);
newWindow.on('closed', function() {
newWindow = null;
});
}
当我关闭主窗口时,我希望所有其他打开的窗口也关闭。我一直很难实现这一点(两个窗口都在主进程中,所以据我所知我不能使用 IPC。)然后我意识到如果我在主窗口时调用 app.quit()关闭它关闭所有其他窗口:
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
// quitting the app on main window close will close any other open windows
app.quit();
})
我的问题是这是否是一件坏事。它会在没有任何用户输入的情况下终止所有打开的窗口,但不会丢失任何未保存的工作,因为所有新窗口都是无法编辑的 pdf。
【问题讨论】:
-
如果下面的答案对您有所帮助,请务必点赞/接受,以帮助可能遇到相同问题的其他人!