我是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
命令时,它显示了这个
而不是此结果
每次我将index.html
拖到第一张图片的空白区域,以获得第二张图片的结果。
我不知道自己做错了什么。请帮助我在这个简单的应用程序中错误的地方。任何形式的帮助将不胜感激。
答案 0 :(得分:7)
我必须运行electron index.html
来解决此问题。
答案 1 :(得分:5)
您必须将package.json所在的目录作为参数传递给electron命令。 因此,如果您从应用程序目录中执行电子,则必须编写
electron .
答案 2 :(得分:3)
如果您在运行“电子”时没有提供任何参数。命令,将main.js重命名为index.js(电子默认运行)