Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

node.js - How to close electron app via javascript?

I am running an express app via electron.

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();
          }
        });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use

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

to close your app.

if you are using jQuery

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

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...