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

前端之家收集整理的这篇文章主要介绍了如何通过javascript关闭电子应用程序?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我通过电子运行快递应用程序.

下面是main.js

  1. const electron = require("electron"),app = electron.app,BrowserWindow = electron.BrowserWindow;
  2.  
  3. let mainWindow;
  4.  
  5. function createWindow () {
  6. mainWindow = new BrowserWindow({
  7. width: 1200,height: 800,frame: false,kiosk: true
  8. });
  9. mainWindow.loadURL(`file://${__dirname}/index.html`)
  10. mainWindow.webContents.openDevTools();
  11.  
  12. mainWindow.on("closed",function () {
  13. mainWindow = null;
  14. })
  15. }
  16.  
  17. app.on("ready",createWindow);
  18. app.on("browser-window-created",function(e,window) {
  19. window.setMenu(null);
  20. });
  21.  
  22. app.on("window-all-closed",function () {
  23. if (process.platform !== "darwin") {
  24. app.quit();
  25. }
  26. });
  27.  
  28.  
  29.  
  30. app.on("activate",function () {
  31. if (mainWindow === null) {
  32. createWindow();
  33. }
  34. });

以下是视图中的一个按钮,点击后我希望它关闭应用程序

  1. <button id="close-btn"><i class="fa fa-cube" aria-hidden="true"></i>&nbsp; Close application</button>

基本上我想在点击按钮后激活这部分代码

  1. app.on("window-all-closed",function () {
  2. if (process.platform !== "darwin") {
  3. app.quit();
  4. }
  5. });

解决方法

您可以使用
  1. const remote = require('electron').remote
  2. let w = remote.getCurrentWindow()
  3. w.close()

关闭你的应用程序.

如果你使用jQuery

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

如果你使用Vue.js

  1. <template>
  2. <button @click="close"><i class="fa fa-cube" aria-hidden="true"></i>&nbsp; Close application</button>
  3. </template>
  4.  
  5. <script>
  6. const remote = require('electron').remote
  7.  
  8. export default{
  9. data(){
  10. return {
  11. w: remote.getCurrentWindow(),}
  12. },methods: {
  13. close() {
  14. this.w.close()
  15. }
  16. }
  17. }
  18. </script>

猜你在找的JavaScript相关文章