如何通过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>&nbsp; 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>&nbsp; Close application</button>
</template>

<script>
    const remote = require('electron').remote

    export default{
        data(){
            return {
                w: remote.getCurrentWindow(),}
        },methods: {
            close() {
                this.w.close()
            }
        }
    }
</script>
原文链接:https://www.f2er.com/js/159028.html

猜你在找的JavaScript相关文章