我通过电子运行快递应用程序.
下面是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()
关闭你的应用程序.
如果你使用jQuery
- const remote = require('electron').remote
- $('#close-btn').on('click',e => {
- remote.getCurrentWindow().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>