如何通过javascript关闭电子应用程序?
问题描述:
我正在通过电子运行一个快速应用程序。如何通过javascript关闭电子应用程序?
下面是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();
}
});
以下是在视图中一个按钮,用户点击后我想它关闭应用
<button id="close-btn"><i class="fa fa-cube" aria-hidden="true"></i> Close application</button>
基本上我想激活这部分一旦按钮被点击的代码
app.on("window-all-closed", function() {
if (process.platform !== "darwin") {
app.quit();
}
});
答
可以使用
const remote = require('electron').remote
let w = remote.getCurrentWindow()
w.close()
要关闭您的应用程序。
如果你是,如果你使用的是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>
答
我用下面的线,关闭应用程序使用jQuery
const remote = require('electron').remote
$('#close-btn').on('click', e => {
remote.getCurrentWindow().close()
})
。
window.close()
我正在使用句柄,但会尝试使用jQuery – John