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

IT技术 javascript node.js express npm electron
2021-02-21 07:56:03

我正在通过电子运行快速应用程序。

下面是 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();
          }
        });
4个回答

您可以使用

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>
@AndroidDev 您的回答也是错误的:如果还没有应用程序,它就不起作用。当我的 Electron 应用程序既是 GUI 又是 CLI 时,我使用 process.exit(),除非指明了 GUI,否则它不会创建应用程序。您仍然需要终止 Electron 进程本身。
2021-05-03 07:56:03
我正在使用把手,但会尝试使用 jquery
2021-05-09 07:56:03
这是错误的。这只是关闭浏览器窗口。在 Mac 上,该应用程序将继续运行。在 Windows 上,它会关闭,但前提是所有窗口都已关闭。你需要使用 app.close()
2021-05-16 07:56:03

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

window.close()

您还可以使用app.exit

app.exit(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 !== 'darwin') {
    app.quit();
  }
});

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

drawin? 你的意思是darwin
2021-04-23 07:56:03
阅读完整的问题,然后给出建议。您可以建议app.exit(0)在现有点击事件中使用。
2021-05-13 07:56:03