How to close electron app via javascript?

前端 未结 4 984
终归单人心
终归单人心 2020-12-18 19:49

I am running an express app via electron.

Below is the main.js

   const electron = require(\"electron\"),
          app = electron.app,
          Bro         


        
相关标签:
4条回答
  • 2020-12-18 19:58

    You can also use 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!)

    0 讨论(0)
  • 2020-12-18 20:16

    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>
    
    0 讨论(0)
  • 2020-12-18 20:21

    I use the line below to close the app.

    window.close()

    0 讨论(0)
  • 2020-12-18 20:22

    in 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;

    0 讨论(0)
提交回复
热议问题