如何通过javascript关闭电子应用程序? [英] How to close electron app via javascript?

查看:108
本文介绍了如何通过javascript关闭电子应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过电子运行快递应用。

I am running an express app via electron.

下面是main.js

Below is the 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>&nbsp; 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();
          }
        });


推荐答案

您可以使用

const remote = require('electron').remote
let w = remote.getCurrentWindow()
w.close()

关闭应用程序。

如果您是使用jQuery

if you are using jQuery

const remote = require('electron').remote
$('#close-btn').on('click', e => {
    remote.getCurrentWindow().close()
})

如果您使用的是Vue.js

if you are using 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>

这篇关于如何通过javascript关闭电子应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆