No *_*ons 3 javascript electron
我正在电子中编写一个应用程序,如果用户打开了未保存的文件,我想在保存之前提示用户。我在网上找到了这个示例代码:
window.onbeforeunload = (e) => {
var answer = confirm('Do you really want to close the application?');
e.returnValue = answer; // this will *prevent* the closing no matter what value is passed
if(answer) { mainWindow.destroy(); } // this will close the app
};
Run Code Online (Sandbox Code Playgroud)
如果在对话框出现后几秒钟内按下“是”、“取消”或“X”按钮,则此代码会奇怪地工作,但如果让对话框在屏幕上停留一会儿,然后单击按钮,则无论按下什么按钮,应用程序都会关闭。
该代码位于我的主脚本文件中,由index.html调用
真是奇怪的行为!我无法解释为什么会发生这种情况,但可以为您提供在主流程中实施的解决方法。
您可以使用电子dialog模块并创建与电子相同的确认对话框。这按预期工作。
main.js
const { app, BrowserWindow, dialog } = require('electron')
const path = require('path')
app.once('ready', () => {
let win = new BrowserWindow()
win.loadURL(path.resolve(__dirname, 'index.html'))
win.on('close', e => {
let choice = dialog.showMessageBox(
win,
{
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Do you really want to close the application?'
}
)
if (choice === 1) e.preventDefault()
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4049 次 |
| 最近记录: |