🦐 Electron 是一个用于构建桌面应用程序的开发框架(Chromium + Node.js + Native API)。
初始化工程
1 2 3
| npm init -y
npm i electron -D
|
package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| { "name": "ele", "version": "1.0.0", "main": "main.js", "scripts": { "start": "electron ." }, "keywords": [], "author": "Ifer", "license": "ISC", "description": "Hello World", "devDependencies": { "electron": "^37.2.5" } }
|
创建主进程
main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| const { app, BrowserWindow } = require("electron");
const createWindow = () => { const mainWindow = new BrowserWindow({ width: 800, height: 600, autoHideMenuBar: true, x: 0, y: 0, alwaysOnTop: true, }); };
app.on("ready", () => { createWindow() app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) createWindow(); }); });
app.on("window-all-closed", () => { if (process.platform !== "darwin") app.quit(); });
|
加载新页面
main.js
1 2 3 4 5 6 7 8 9 10 11 12 13
| function createWindow() { const win = new BrowserWindow({ width: 800, height: 600, autoHideMenuBar: true, x: 0, y: 0, alwaysOnTop: true }); win.loadFile("./pages/index.html"); }
|
pages/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'" /> <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'" /> <title>来自 Electron 渲染器的问好!</title> </head> <body> Hello World </body> </html>
|
重启新窗口
package.json
1 2 3 4 5 6
| { "scripts": { "start": "nodemon --exec electron ." } }
|
nodemon.json
1 2 3 4 5 6 7 8 9 10
| { "ignore": [ "node_modules", "dist" ], "restartable": "r", "watch": ["*.*"], "ext": "html,js,css" }
|
预加载脚本
为了将不同类型的进程桥接在一起,我们需要使用被称为「预加载」的特殊脚本,例如需要将主进程的 API 暴漏给渲染进程。
preload.js
1 2 3 4 5 6 7 8
| const { contextBridge } = require('electron')
contextBridge.exposeInMainWorld('versions', { node: () => process.versions.node, chrome: () => process.versions.chrome, electron: () => process.versions.electron })
|
在 BrowserWindow 构造器中使用 webPreferences.preload 传入脚本的路径。
1 2 3 4 5 6 7 8 9 10 11
| const win = new BrowserWindow({ width: 800, height: 600, autoHideMenuBar: true, x: 0, y: 0, alwaysOnTop: true, webPreferences: { preload: path.join(__dirname, 'preload.js') } });
|
pages/renderer.js
1 2
| const information = document.getElementById("info"); information.innerText = `本应用正在使用 Chrome (v${versions.chrome()}), Node.js (v${versions.node()}), 和 Electron (v${versions.electron()})`;
|
pages/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'" /> <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'" /> <title>来自 Electron 渲染器的问好!</title> </head> <body> Hello World <div id="info"></div> <script src="./renderer.js"></script> </body> </html>
|
进程间通信
https://www.electronjs.org/zh/docs/latest/tutorial/ipc
渲染进程到主进程(单)
渲染进程到主进程(双)
1. 主进程
中通过 ipcMain.handle() 监听事件并返回数据给预加载脚本触发的地方;
2. 预加载脚本
中定义函数,通过 ipcRenderer.invoke() 触发主进程中监听的事件,拿到主进程返回的数据,并返回数据给渲染进程;
3. 渲染进程
中通过调用预加载脚本中暴漏的函数拿到数据。
主进程到渲染进程
渲染进程到渲进程
https://www.electronjs.org/zh/docs/latest/tutorial/ipc#%E6%A8%A1%E5%BC%8F-3%E4%B8%BB%E8%BF%9B%E7%A8%8B%E5%88%B0%E6%B8%B2%E6%9F%93%E5%99%A8%E8%BF%9B%E7%A8%8B
项目的打包
1
| npm i electron-builder -D
|
package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| { "name": "ele", "version": "1.0.0", "main": "main.js", "scripts": { "start": "nodemon --exec electron .", "build": "electron-builder" }, "keywords": [], "author": "Ifer", "license": "ISC", "description": "Hello World", "devDependencies": { "electron": "^37.2.5", "electron-builder": "^26.0.12", "nodemon": "^3.1.4" }, "build": { "appId": "com.electron.ele", "win": { "icon": "./logo.ico", "target": [{ "target": "nsis", "arch": ["x64"] }] }, "nsis": { "oneClick": false, "perMachine": true, "allowToChangeInstallationDirectory": true } } }
|
Electron Vite
https://cn.electron-vite.org/guide/introduction