javascript – 电子快速启动应用程序无法正常工作

我是电子新手,并试图遵循不同的教程.目前我正在关注此链接到Write my First Electron 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>

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

this

而不是这个结果

instead

每次我将index.html拖到第一个图像的空白区域以获得第二个图像的结果.

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

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

electron .

转载注明原文:javascript – 电子快速启动应用程序无法正常工作 - 代码日志