How to close electron app via javascript?

时间:2017-04-10 02:09:12

标签: javascript node.js express npm electron

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();
          }
        });

4 个答案:

答案 0 :(得分:18)

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>

答案 1 :(得分:7)

我使用下面的行关闭应用。

$(function() { $.getJSON("./test.json", function(json) { var opener = '<!-- Blog Start --><div class="blog_post"><div class="blog_panel">'; var closer = '</div></div><!-- Blog End -->'; $(json.blogs).each(function() { var body = '' + '<img class="blog_image" src="folder/' + this.img + '" alt="' + this.title + '">' + '<div class="blog_panel_body">' + '<h2 class="blog_title">' + this.title + '</h2>' + '<span class="blog_author_date">' + this.author + ':' + this.date + '</span>' + '<p class="blog_content">' + this.content + '</p>' + '</div>' ; $('.blog_list').append(opener + body + closer); }) }); })

答案 2 :(得分:0)

在main.js中

function createWindow(){
win =new BrowserWindow({
    icon:'./img/code_file.ico',
    frame:false
});
win.maximize();

win.loadURL(url.format({
    pathname:path.join(__dirname,'./login.html'),
    protocol:'file:',
    slashes:true
}));
  // Emitted when the window is closed.
  win.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  });
}
app.on('ready',createWindow);
app.on('Window-all-closed',()=>{
if(process.platfrom!=='drawin'){
app.quit();
}
});
}

当所有窗口关闭时,app.quit()函数关闭应用程序;

答案 3 :(得分:0)

您也可以使用app.exit

app.exit(0)

这似乎绕过了所有事件侦听器,并迫使整个应用立即退出。 (请确保那是您想要的!)