电子快速启动应用程序不工作

时间:2016-04-03 19:12:00

标签: javascript html angularjs json electron

我是electron的新手,并尝试按照不同的教程进行操作。目前,我已关注此Write my First Electron App

的链接

我的app结构如下

your-app/
 ├── package.json
 ├── main.js
 └── index.html

package.json的格式为

 {
  "name"    : "your-app",
  "version" : "0.1.0",
  "main"    : "main.js"
}

这是我的main.js

'use strict';

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

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform != 'darwin') {
    app.quit();
  }
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600});

  // and load the index.html of the app.
  mainWindow.loadURL('file://' + __dirname + '/index.html');

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

  // Emitted when the window is closed.
  mainWindow.on('closed', function() {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });
});

index.html是我想要展示的网页:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

现在,当我在我的应用程序的源目录中运行electron命令时,它显示了这个

this

而不是此结果

instead

每次我将index.html拖到第一张图片的空白区域,以获得第二张图片的结果。

我不知道自己做错了什么。请帮助我在这个简单的应用程序中错误的地方。任何形式的帮助将不胜感激。

3 个答案:

答案 0 :(得分:7)

我必须运行electron index.html来解决此问题。

答案 1 :(得分:5)

您必须将package.json所在的目录作为参数传递给electron命令。 因此,如果您从应用程序目录中执行电子,则必须编写

electron .

答案 2 :(得分:3)

如果您在运行“电子”时没有提供任何参数。命令,将main.js重命名为index.js(电子默认运行)