Electron 可以让你使用纯 JavaScript 调用丰富的原生APIs 来创造桌面应用
整个项目的目录结构
your-app/
├── package.json
├── main.js
├─── index.html
└── out // 整个是打包后***.exe文件的输出位置
package.json
{
"name": "first_electron",
"version": "0.0.1",
"description": "",
"main": "main.js",
"scripts": {
"start": "node_modules/.bin/electron .",
"packages": "electron-packager . HelloWorld --platform=win32 --arch=x64 --out=./out --app-version=0.0.1 --overwrite --ignore=node_modules"
},
"author": "",
"license": "ISC",
"dependencies": {
"electron": "^16.0.6",
"electron-prebuilt": "^1.4.13"
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using io.js <script>document.write(process.version)</script>
and Electron <script>document.write(process.versions['electron'])</script>.
</body>
</html>
main.js
var {app , BrowserWindow} = require('electron'); // 控制应用生命周期的模块。
// var BrowserWindow = require('browser-window'); // 创建原生浏览器窗口的模块
// 保持一个对于 window 对象的全局引用,不然,当 JavaScript 被 GC,
// window 会被自动地关闭
var mainWindow = null;
app.addRecentDocument('D:\lxw');
// 当所有窗口被关闭了,退出。
app.on('window-all-closed', function() {
// 在 OS X 上,通常用户在明确地按下 Cmd + Q 之前
// 应用会保持活动状态
if (process.platform != 'darwin') {
app.quit();
}
});
// 当 Electron 完成了初始化并且准备创建浏览器窗口的时候
// 这个方法就被调用
app.on('ready', function() {
// 创建浏览器窗口。
mainWindow = new BrowserWindow({width: 900, height: 600});
// 加载应用的 index.html
mainWindow.loadURL('file://' + __dirname + '/index.html');
// 打开开发工具
mainWindow.openDevTools();
// 当 window 被关闭,这个事件会被发出
mainWindow.on('closed', function() {
// 取消引用 window 对象,如果你的应用支持多窗口的话,
// 通常会把多个 window 对象存放在一个数组里面,
// 但这次不是。
mainWindow = null;
});
mainWindow.loadURL('http://10.0.76.159')
})
运行命令
yarn start // 运行程序
yarn run packages // 打包成**.exe 文件
运行结果