无法用电子打开我的BrowserWindow?

时间:2018-02-17 11:53:23

标签: javascript html electron

我是Electron的新手,我按照文档使用电子创建了我的第一个hello world应用程序,但是我有这个错误:

> tofita@1.0.0 start C:\Users\user\Desktop\Electron\elc
> electron .


App threw an error during load
Error: Cannot create BrowserWindow before app is ready
at createWindow (C:\Users\user\Desktop\Electron\elc\main.js:10:11)
at Object.<anonymous> (C:\Users\user\Desktop\Electron\elc\main.js:23:16)
at Object.<anonymous> (C:\Users\user\Desktop\Electron\elc\main.js:41:3)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at loadApplicationPackage(C:\Users\user\Desktop\Electron\elc\node_modules\electron\dist\resources\default_app.asar\main.js:287:12)
at Object.<anonymous> (C:\Users\user\Desktop\Electron\elc\node_modules\electron\dist\resources\default_app.asar\main.js:329:5)

弹出窗口告诉我同样的事情,而且我不知道如何解决问题。

那是我的文件main.js:

const { app, BrowserWindow } = require('electron')
const path = require('path')
const url = require('url')

let win;

function createWindow() {
  win = new BrowserWindow({width:200,height:300});
  win.loadURL(url.format({
    pathname: path.join(__dirname,'index.html'),
    protocol: 'file:',
    slashes: true
  }));
win.webContents.openDevTools();

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

app.on('ready',createWindow())

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


 app.on('activate', () => {
    if (win === null) {
      createWindow()
      }
    })

那是我的HTML代码:

<!DOCTYPE html>
<html lang="en">
   <head>
     <meta charset="UTF-8">
     <title>Electron Quick Start</title>
   </head>
 <body>
   <h1>Hello World</h1>
 </body>
</html>

那是我的文件package.json:

{
"name": "tofita",
"version": "1.0.0",
"description": "tofita",
"main": "main.js",
"scripts": {
    "start": "electron ."
},
"author": "Tofita",
"license": "ISC",
  "dependencies": {
    "electron": "^1.8.2"
  }
}

执行'npm start'命令后出现错误。

我希望我的问题得到很好的解释,任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:5)

您正在执行createWindow函数,您必须将其作为不调用函数的引用传递

app.on("ready", ..... )

这样做

app.on("ready", createWindow);