电子关闭按钮不起作用 [英] Electron close button not working

查看:56
本文介绍了电子关闭按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Electron(以前是Atom Shell)创建一个应用程序.该应用程序包装了AngularJS应用程序,并与在nodejs中创建的端点进行交互以编辑和保存HTML内容.我能够创建应用程序而没有任何问题.

I am trying to create an application using Electron (formerly Atom Shell). This application wraps an AngularJS application and interacts with endpoints created in nodejs to edit and save the HTML content. I am able to create the application with no issues.

当我尝试通过电子访问"/saveContent"时,关闭按钮(Windows右上角关闭)变得无响应,但是最小化和最大化工作正常而没有问题.如果我通过电子访问任何其他端点,则不会出现此问题.我已经尝试了同步文件写入和其他方式.因此,我认为main.js中的"/saveContent"是导致此问题的原因.

When I try to access "/saveContent" from electron causes close button (Windows close on top right corner) to become unresponsive, however minimize and maximize works fine without issue. If I access any other endpoint through electron this issue doesn't come up. I have tried with both sync file write and otherwise too. So I assume "/saveContent" in main.js is cause of the issue.

如果我在"Windows任务管理器"中结束了node.exe,则会关闭整个应用程序.

If I end node.exe in "Windows task Manager" this closes the whole application.

我在下面有主要的过程代码

I have the main process code below

'use strict';
var fs = require("fs");
const util = require('util')

var cheerio = require("cheerio");

var express = require('express');
var exapp = express();

var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({extended: false});
exapp.use(bodyParser.json());

const electron = require('electron');
const app = electron.app;  // Module to control application life.
const BrowserWindow = electron.BrowserWindow;  // Module to create native browser window.
const path = require('path')
const url = require('url')
var mainWindow = null;



app.on('ready', function() {
  mainWindow = new BrowserWindow({width: 1280, height: 900, title: "2018JL", "nodeIntegration":false});
  //mainWindow.loadURL(__dirname + '/app/index.html');
  mainWindow.loadURL('http://localhost:5001/');

  mainWindow.on('closed', function() {
    mainWindow = null;
  });
});

app.on('window-all-closed', function() {
    if (process.platform != 'darwin') {
        app.quit();
    }
});


exapp.get('/editPage', function(req,res){
    if(req){
    //console.log("req.query.editURL  "+ req.query.editURL);
        var url = req.query.editURL;
        var editURL = path.join(__dirname + '/app/views'+ url+".html");

        fs.exists(editURL, function(fileok){
            if(fileok){
            fs.readFile(editURL, 'utf8', function (err, data) {
               if (err) {
                    console.log("error.... "+ err);
                    return console.error(err);
               }
               //console.log("data "+ editURL);
               res.send(JSON.stringify({path:editURL, content:data}));
            });
            }else{
                console.log("file not found");
            }
        });
    }
});

exapp.post('/saveContent', function (req, res) {
    //console.log(util.inspect(req, false, null))
    if (req) {
        //console.log(req.query.url + " ------  " + req.query.content);

        var $ = cheerio.load(req.query.content);
        var htmlContent = $('body').children();

        console.log('htmlContent  '+htmlContent);

        fs.writeFile(req.query.url, htmlContent,  function(err) {
           if (err) {
               res.send("Error");
           }
           console.log("End of write file");
           res.send("success");
        });
    }
    console.log("End of function .....");
});

exapp.get('/test', function (req, res) {
    res.send("Test success ");
});


exapp.use(express.static(__dirname + '/app'));
exapp.listen(process.env.PORT || 5001);

下面的客户代码

$scope.editPage = function () {
            $http({method: "GET",
                url: "/editPage",
                params: {editURL: $location.path()}
            }).then(function success(response) {
                //var result = JSON.parse(response.data);
                //console.log("HTTP Success "+response.data.path);
                $scope.showEditor = true;
                $scope.editURL = response.data.path;
                tinymce.get('contentEditor').setContent(response.data.content);
            }, function error(response) {
                console.log("HTTP Error " + response.statusText);
            });
        };

在'/saveContent'中注释文件写入代码不会导致电子关闭按钮无响应.

Commenting file write code in '/saveContent' doesn't cause electron close button to become unresponsive.

推荐答案

我将mainWindow的代码替换为如下所示,并且工作正常

I replace the code for mainWindow close to as below and works fine

    mainWindow.on('close', function(e) { 
        e.preventDefault();
        mainWindow.destroy();
    });

https://github.com/电子/电子/blob/master/docs/api/browser-window.md#windestroy

这篇关于电子关闭按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆