window 的 beforeunload 事件提示在electron 不兼容,弹窗提示不出来,还会导致莫名其妙的假死问题,下面记录一下解决方法。
- win.webContents.on('will-prevent-unload', (event) => {
- event.preventDefault(); // 忽略 beforeunload 事件
- });
- win.webContents.on('will-prevent-unload', (event) => {
- const choice = dialog.showMessageBoxSync(win, {
- type: 'question',
- buttons: ['Leave', 'Stay'],
- title: 'Do you want to leave this site?',
- message: 'Changes you made may not be saved.',
- defaultId: 0,
- cancelId: 1,
- });
- if (choice === 0) {
- event.preventDefault(); // 忽略 beforeunload 事件
- }
- });
- }
以上。