【问题标题】:Electron app runs without any errors however the window does not open or show in task managerElectron 应用程序运行没有任何错误,但窗口没有打开或在任务管理器中显示
【发布时间】:2021-11-18 20:57:04
【问题描述】:

我正在构建一个电子应用程序。该应用程序运行没有任何错误,但没有打开。我在 32 位机器上运行 Windows 7。我的 main.js 文件如下所示:

const {app, BrowserWindow} = require('electron');
const path = require('path');
const url = require('url');
// Initialize window
let win;

function createWindow() {

     win = new BrowserWindow({
        width: 800,
        height: 600,
        icon: __dirnaname+ '/assets/images/icon.jpg'
    });

    // Load Window
    win.loadUrl(url.format({
        pathname: path.join(__dirname, './index.html'),
        protocol: 'file',
        slashes: true
    }));

    // Close window
    win.on('closed', () =>{
        win = null;
    });

    //Run Create Window Function
    win.on('ready', createWindow);

    //Check Mac OS platform
    app.on('all-window-closed', () => {
        if(process.platform !== 'darwin') {
            app.quit();
        }
    });

};

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:

    这一行是错误的

    win.on('ready', createWindow);
    

    你是说

    app.on('ready', createWindow);
    

    createWindow 的范围之外。像这样:

    function createWindow() {
      ...
    };
    app.on('all-window-closed', () => {
      ...
    });
    app.on('ready', createWindow);
    

    【讨论】:

    • 我试过了,但它抛出一个错误:'Cannot read porperty 'on' of null'
    • app 应该是有效的,因为您需要 const { app } = require('electron')
    • 感谢 cmets 我想出了问题所在。我省略的 loadUrl 协议:在文件前面应该是协议:'file:'
    【解决方案2】:

    在我将电子从 v3.x 升级到 v8.x 后,我遇到了同样的问题,它开始工作了

    【讨论】:

      猜你喜欢
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多