'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;
});
});
迷之心声:好长啊,幸好我是复制黏贴的。这里面的代码有点类似 Phonegap 启动页呢。
用于绘制界面的 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 .
由于下载的是 Windows 下的 exe 文件,所以就通过设置环境变量直接调用了 electron 命令。
又简单得尝试了 Electron
这篇文章接上一篇,这里主要讲讲学习 Electron 的一些心得。
关于 Electron
Electron 和 node-webkit 属于同类型产品,都致力于使用 Web 技术开发不同操作系统上的本地应用。而且他们都是由 Node.js 和 Chromium 结合而成。
另外与 node-webkit 类似的是,Electron 也有过其他名字——Atom Shell,不同的是 node-webkit 这个名字已经是过去时,现在叫 NW。
关于 Electron 与 node-webkit 的不同之处,官方有一篇文章做出了回答,戳这里
libchromiumcontent
打包;运行
继续以 hello world 为例。
在下载完 Electron 后,编写 3 个文件,嗯,这比 node-webkit 多一份文件嘛!
配置文件 package.json
用于运行应用的 main.js
用于绘制界面的 index.html
命令行执行
由于下载的是 Windows 下的 exe 文件,所以就通过设置环境变量直接调用了
electron
命令。效果如图:
打包
Electron 的打包方式可以使用
electron-packager
完成。然后执行
electron-packager
命令。打包完成的 app 会放在
<out>/<appname>-<platform>-<arch>
目录下,这个路径可以通过optional flags
修改。optional flags 配置说明
Thanks