Javascript 如何通过javascript关闭电子应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43314039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close electron app via javascript?
提问by John
I am running an express app via electron.
我正在通过电子运行快速应用程序。
Below is the main.js
下面是 main.js
const electron = require("electron"),
app = electron.app,
BrowserWindow = electron.BrowserWindow;
let mainWindow;
function createWindow () {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
frame: false,
kiosk: true
});
mainWindow.loadURL(`file://${__dirname}/index.html`)
mainWindow.webContents.openDevTools();
mainWindow.on("closed", function () {
mainWindow = null;
})
}
app.on("ready", createWindow);
app.on("browser-window-created",function(e,window) {
window.setMenu(null);
});
app.on("window-all-closed", function () {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", function () {
if (mainWindow === null) {
createWindow();
}
});
and below is a button in the view that upon click i would like it to close the app
下面是视图中的一个按钮,点击后我希望它关闭应用程序
<button id="close-btn"><i class="fa fa-cube" aria-hidden="true"></i> Close application</button>
Essentially I want to activate this part of the code once the button has been clicked
基本上我想在点击按钮后激活这部分代码
app.on("window-all-closed", function () {
if (process.platform !== "darwin") {
app.quit();
}
});
回答by Frank He
you can use
您可以使用
const remote = require('electron').remote
let w = remote.getCurrentWindow()
w.close()
to close your app.
关闭您的应用程序。
if you are using jQuery
如果你使用 jQuery
const remote = require('electron').remote
$('#close-btn').on('click', e => {
remote.getCurrentWindow().close()
})
if you are using Vue.js
如果你使用 Vue.js
<template>
<button @click="close"><i class="fa fa-cube" aria-hidden="true"></i> Close application</button>
</template>
<script>
const remote = require('electron').remote
export default{
data(){
return {
w: remote.getCurrentWindow(),
}
},
methods: {
close() {
this.w.close()
}
}
}
</script>
回答by Alocus
I use the line below to close the app.
我使用下面的行关闭应用程序。
window.close()
window.close()
回答by Kip
回答by Abhi
in main.js
在 main.js 中
function createWindow(){
win =new BrowserWindow({
icon:'./img/code_file.ico',
frame:false
});
win.maximize();
win.loadURL(url.format({
pathname:path.join(__dirname,'./login.html'),
protocol:'file:',
slashes:true
}));
// Emitted when the window is closed.
win.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.
win = null
});
}
app.on('ready',createWindow);
app.on('Window-all-closed',()=>{
if(process.platfrom!=='drawin'){
app.quit();
}
});
}
when all window is closed app.quit() function closed the application;
当所有窗口关闭时 app.quit() 函数关闭应用程序;

