SKu*_*ers 4 javascript event-handling electron
我有一个应用程序需要在退出之前进行 API 调用(类似于注销)。由于我仍然需要访问一些应用程序数据(redux 存储)以进行 API 调用,因此我决定监听应用程序上的“before-quit”事件。
我尝试了以下代码:
import {remote} from 'electron';
let loggedout = false;
remote.app.on('before-quit', (event) => {
if (loggedout) return; // if we are logged out just quit.
console.warn('users tries to quit');
// prevent the default which should cancel the quit
event.preventDefault();
// in the place of the setTimout will be an API call
setTimeout(() => {
// if api call was a success
if (true) {
loggedout = true;
remote.app.quit();
} else {
// tell the user log-out was not successfull. retry and quit after second try.
}
}, 1000);
});
Run Code Online (Sandbox Code Playgroud)
该事件似乎永远不会触发或阻止关闭不起作用。当我替换before-quit
为browser-window-blur
事件时,它会触发并且代码似乎可以工作。
作为参考,我使用 Electron 1.2.8(由于某些依赖项,我无法升级)。我已经仔细检查过,before-quit
事件已经在该版本中实现了。
有什么想法为什么这个事件似乎没有被解雇吗?
提前致谢,节日快乐!
我有同样的问题,这是我的解决方案:
在渲染器中:
const { ipcRenderer } = require('electron')
window._saved = false
window.onbeforeunload = (e) => {
if (!window.saved) {
callSaveAPI(() => {
// success? quit the app
window._saved = true
ipcRenderer.send('app_quit')
window.onbeforeunload = null
})
}
e.returnValue = false
}
Run Code Online (Sandbox Code Playgroud)
主要内容:
const { ipcMain } = require('electron')
// listen the 'app_quit' event
ipcMain.on('app_quit', (event, info) => {
app.quit()
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9225 次 |
最近记录: |