简体   繁体   English

如何通过javascript关闭electron应用程序?

[英]How to close electron app via javascript?

I am running an express app via electron.我正在通过 electron 运行一个快速应用程序。

Below is the main.js下面是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();
          }
        });

you can use您可以使用

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

to close your app.关闭您的应用程序。

if you are using jQuery如果你使用 jQuery

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

if you are using Vue.js如果你使用 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>

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

window.close()

You can also use app.exit :您还可以使用app.exit

app.exit(0)

This seems to bypass any event listeners and force the whole app to exit immediately.这似乎绕过了任何事件侦听器并强制整个应用程序立即退出。 (Be sure that is what you want though!) (请确保这是您想要的!)

It is wrong way, If you run window.close(), it just close window that app still run background.这是错误的方式,如果您运行 window.close(),它只会关闭 window 该应用程序仍在后台运行。

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

You should close app like this:你应该像这样关闭应用程序:

 const remote = require('electron').remote let app = remote.app app.quit()

in main.js在 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();
}
});
}

when all window is closed app.quit() function closed the application;当所有窗口关闭时 app.quit() 函数关闭应用程序;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM