經(jīng)過兩天的各種文檔查找,最終放棄第一種插佛,只能選擇beforeUnload方式來處理。
失敗的一種方式:
vue中
methods:
closeServe(pid) {
const execClose = `taskkill /pid ${pid} -t -f`;
$childProcess.exec(execClose, (error, stdout, stderr) => {
if (error) {
console.log(error.stack);
console.log('Error code: '+error.code);
return;
}
})
}
mounted:
$remote.getCurrentWindow().on('close', () => {
this.closeServe(this.projectList[0].execChild);
$remote.getCurrentWindow().removeAllListeners();
})
成功的方式:
let closeWindow = false
window.addEventListener('beforeunload', evt => {
if (closeWindow) return
logFile(`[currentWindow beforeunload]`);
evt.returnValue = false
setTimeout(() => {
let result = $dialog.showMessageBox({
message: '是否確認退出應用?',
buttons: ['是', '否']
})
logFile(`[currentWindow beforeunload result] ${result}`);
if (result == 0) {
closeWindow = true
if(this.projectList.length) {
this.projectList.forEach((item, index) => {
//不管是否啟動都執(zhí)行
item.isActive = false;
localStorage.setItem('auto_project_collection', JSON.stringify(this.projectList));
logFile(`[currentWindow close forEach] ${item.isActive}`);
this.closeServe(item, index, true);
});
}
$currentWindow.close();
$currentWindow.removeAllListeners('close');
}
})
})
這個方法需要在入口的vue組件中氢拥,否則會重復多次監(jiān)聽嫩海,導致多次出現(xiàn)是否關閉的提示彈窗囚痴。