一.安装
1.安装nodejs
由于electron是基于nodejs的,所以我们首先要安装nodejs
官网地址:https://nodejs.org/en/
直接下载安装即可;看安装是否成功命令:node -v,npm -v ;出现版本号即表示安装成功
2.安装electron
2.1由于直接访问国外服务器缓慢,我选择先安装淘宝镜像的包命令行管理工具cnpm
命令:npm install cnpm -g --registry=http://registry.npm.taobao.org
2.2再安装electron
命令:cnpm install electron -g
2.3校验是否安装成功
命令:electron -v ;出现版本号表示安装成功
3.安装目录下找到下载好的electron文件
3.1复制在任意位置,解压;
3.2双击electron.exe
红色框意思就是要运行程序就在cmd窗口中输入命令 : electtron.exe path-to-app(app路径)
二.编辑入门程序Hello Word
1.创建文件夹 electron//文件名随意 ; 里面创建三个文件 package.json
,main.js
,index.html
2.编写package.json
{
"name" : "hello Word",
"version" : "0.1.0",
"main" : "main.js"
}
3.编写main.js
const electron = require('electron');
// Module to control application life.
const {app} = electron;
// Module to create native browser window.
const {BrowserWindow} = electron;
let win;
function createWindow() {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
win.loadURL(`file://${__dirname}/index.html`);//注意
win.webContents.openDevTools();
win.on('closed', () => {
win = null;
});
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (win === null) {
createWindow();
}
});
4.编写index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
5.启动项目:
在electron-v2.0.0-win32-x64 解压包里面
在此处运行 输入命令: electron.exe C:\Users\muhua\Desktop\electron //electron.exe 运行文件路径
app.commandLine.appendSwitch("--disable-http-cache");//禁止使用缓存,这是我在项目中需要增加的;