在安装Electron的时候,因为访问的是国外的IP,所以很容安装失败,需要设置访问国内的Electron镜像:yarn config set ELECTRON_MIRROR http://cdn.npm.taobao.org/dist/electron/
安装ELectron:yarn add electron --dev --platform=win64
在package.json文件中设置:
{ "name": "electron", "version": "1.0.0", "main": "index.js", "license": "MIT", "scripts": { "start": "electron ./index.js" }, "devDependencies": { "electron": "^16.0.7" } }
然后在项目目录下创建index.js和index.html:
index.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>electron</title> </head> <body> electron is 桌面应用 </body> </html>
index.js:
let electron = require('electron'); let app = electron.app; let BrowserWindow = electron.BrowserWindow; let win = null; app.on('ready',()=>{ win = new BrowserWindow({ webPreferences:{ nodeIntegration:true } }); win.loadFile('index.html'); win.on('closed',()=>{ win=null; }) }) app.on('window-all-closed',()=>{ app.quit(); })
执行命令:yarn start
:
效果如图: