【问题标题】:How to close electron app via javascript?如何通过javascript关闭电子应用程序?
【发布时间】:2017-09-04 23:00:07
【问题描述】:

我正在通过 electron 运行一个 express 应用程序。

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

【问题讨论】:

    标签: javascript node.js express npm electron


    【解决方案1】:

    你可以使用

    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>
    

    【讨论】:

    • 我正在使用把手,但会尝试使用 jquery
    • 这是错误的。这只是关闭浏览器窗口。在 Mac 上,应用程序将继续运行。在 Windows 上,它会关闭,但前提是所有窗口都已关闭。你需要使用 app.close()
    • @AndroidDev 你的回答也是错误的:如果还没有应用就不行。当我的 Electron 应用程序既是 GUI 又是 CLI 时,我使用 process.exit(),除非指示 GUI,否则它不会创建应用程序。你仍然需要杀死 Electron 进程本身。
    • 这需要 BrowserWindow() 中的选项 enableRemoteModule: true: new BrowserWindow({ webPreferences: { enableRemoteModule: true, } }
    【解决方案2】:

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

    window.close()

    【讨论】:

      【解决方案3】:

      你也可以使用app.exit:

      app.exit(0)
      

      这似乎绕过了任何事件侦听器并强制整个应用程序立即退出。 (但请确保这是您想要的!)

      【讨论】:

        【解决方案4】:

        在 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() 函数关闭应用程序;

        【讨论】:

        • 阅读完整问题,然后提出建议。你可以建议app.exit(0)在存在点击事件中使用。
        • drawin?你的意思是darwin
        猜你喜欢
        • 1970-01-01
        • 2016-05-16
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        • 2019-07-23
        • 2019-03-29
        • 2019-10-15
        • 2018-01-03
        相关资源
        最近更新 更多