Electron app runs without any errors however the w

2019-08-04 10:41发布

问题:

I am building an electron app. The app runs without any errors but does not open. I am running windows 7 on a 32 bit machine. My main.js file looks like this:

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

};

回答1:

This line is wrong

win.on('ready', createWindow);

you meant

app.on('ready', createWindow);

outside of createWindow's scope. Like this:

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