electron是一个使用JS,CSS,HTML构建跨平台的桌面应用程序的框架,微软的VScode就是使用该技术开发
环境搭建
安装node环境
由于使用的是electron-vue脚手架工具,首先要安装vuecli
1 2 3
| npm install -g @vue/cli
yarn global add @vue/cli
|
- 起步
vue init
阶段会出现一些项目基本选项,按照自己需求选择就好,打包工具选择elelctron-builder
之后会用到它打包和更新

1 2 3 4 5 6 7 8 9 10 11
| vue init simulatedgreg/electron-vue my-project
cd my-project
npm install
yarn
npm run dev
yarn dev
|
- 出现以下界面及成功

注意事项
npm install
时候可能会由于网络原因导致包安装失败,使用cnpm
或者nrm
切换软件源
npm install
时候可能会遇到electron
下载失败情况,有两种解决方法
- 在项目根目录下新建
.npmrc
或者.yarnrc
文件,里面写入1
| electron_mirror=http://npm.taobao.org/mirrors/electron/
|
- 在淘宝镜像下载对应系统和版本的electron包到electron缓存目录
1
| mac/win/liunx: 当前用户名/.electron/
|
- windows用户在
npm install
时候会遇到node-gyp
错误,这是由于你的系统没有安装正确到构建工具,可以使用windows-build-tools一键安装所有到所需工具包
1 2 3
| npm install --global windows-build-tools
yarn global add windows-build-tools
|
npm run dev
时候可能会遇到process is not defined
错误,这是由于node10.X版本导致的,有两种解决方法
- 升级node版本到12.X
- 打开项目
/src/index.ejs
,修改!process.browser
为!require('process').browser
基础入门
插件使用
插件使用和vue一样,npm/yarn直接装包就可以,脚手架自带electron-vuex可以实现主进程与渲染线程共享数据,推荐使用
css预处理器
假设我们使用less作为我们到css预先处理,需要安装less并且修改webpack配置
- 安装
1 2 3
| npm install -D less less-loader
yarn add -D less less-loader
|
- 打开
.electron-vue/webpack.renderer.config.js
,在less rule里面加入less-loader
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
| { test: /\.less$/, use: [ { loader: "vue-style-loader" }, { loader: "css-loader", options: { importLoaders: 1 } }, { loader: "postcss-loader", }, { loader: "less-loader", options: { javascriptEnabled: true, modifyVars: { 'text-primary-color': '#293A4D', 'text-minor-color': 'rgba(41, 58, 77, 0.45)', 'text-highlight-color': '#1890FF' } } } ] }
|
静态资源使用
在项目根目录下的static
目录,可以存放主进程和渲染进程都可以访问到的静态资源,electron-vue
提供一个__static
全局变量来快速访问此目录,而且开发环境和生产都可以使用。
- vue模版使用
1 2 3 4 5 6 7 8 9 10 11 12
| <template> <img v-bind:src="imageUrl"> </template>
<script> export default { data () { return { imageUrl: 'static/imgs/unsplash.png' } } } </script>
|
- js文件使用
1 2 3 4 5 6
| import fs from 'fs' import path from 'path'
let fileContents = fs.readFileSync(path.join(__static, '/someFile.txt'), 'utf8')
console.log(fileContents)
|
读写本地文件
使用 electron 的一大好处是可以访问用户的文件系统。这使你可以读取和写入本地系统上的文件。electron提供了app.getPath(name)来快速访问本地文件目录,比如桌面、用户目录等等
1 2 3 4 5 6 7 8 9 10
| app.getPath('home')
app.getPath('desktop')
app.getPath('downloads')
|
主线程与渲染进程通信
ipcMain
和ipcRenderer
1 2 3 4 5 6 7 8 9 10 11
| const { ipcMain } = require('electron') ipcMain.on('asynchronous-message', (event, arg) => { console.log(arg) event.reply('asynchronous-reply', 'pong') })
ipcMain.on('synchronous-message', (event, arg) => { console.log(arg) event.returnValue = 'pong' })
|
1 2 3 4 5 6 7 8
| const { ipcRenderer } = require('electron') console.log(ipcRenderer.sendSync('synchronous-message', 'ping'))
ipcRenderer.on('asynchronous-reply', (event, arg) => { console.log(arg) }) ipcRenderer.send('asynchronous-message', 'ping')
|
remote
模块
在渲染进程中,可以通过const {remote} = require('electron')
来获取到remote
对象. 通过这个对象可以允许渲染进程访问主进程的模块.
1 2 3 4 5 6 7 8
| const { app } = require('electron');
app.utils = { test () { return 'hello world' } }
|
1 2 3 4
| const {remote} = require('electron') remote.app.utils.test()
|
软件打包
electron-packager
和electron-builder
都可以把代码打包成各个平台安装包。正经过调研和实际使用electron-builder
功能丰富,可配置性强,故下面都使用electron-builder
进行打包
- 在
vue init
阶段应该选择electron-builder
作为打包工具
- 为了方便首先自定义打包命令
为各个平台添加打包命令。在package.json
文件scripts
里加入以下命令
1 2 3 4 5
| "scripts": { "build:dmg": "node .electron-vue/build.js && electron-builder --mac", "build:deb": "node .electron-vue/build.js && electron-builder --linux", "build:exe": "node .electron-vue/build.js && electron-builder --win" }
|
- 自定义打包配置
与打包相关的配置在package.json
文件的build
下面
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 34
| "build": { "productName": "hello-electron-vue", "appId": "com.example.yourapp", "directories": { "output": "build" }, "files": [ "dist/electron/**/*" ], "dmg": { "contents": [ { "x": 410, "y": 150, "type": "link", "path": "/Applications" }, { "x": 130, "y": 150, "type": "file" } ] }, "mac": { "icon": "build/icons/icon.icns" }, "win": { "icon": "build/icons/icon.ico" }, "linux": { "icon": "build/icons" } }
|
上面是默认配置,下面是我的自定义配置并会进行相关说明,更多配置请查看electron-builder
官方文档
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| "build": { "productName": "这里是软件安装后的名字", "appId": "com.example.yourapp", "copyright": "这里是软件版权信息", "directories": { "output": "build" }, "files": [ "dist/electron/**/*" ], "electronDownload": { "mirror": "https://npm.taobao.org/mirrors/electron/" }, "dmg": { "contents": [ { "x": 410, "y": 150, "type": "link", "path": "/Applications" }, { "x": 130, "y": 150, "type": "file" } ], "sign": false, "icon": "build/icons/icon.ico" }, "appImage": { "desktop": { "Name[zh_CN]": "这里是软件安装后的名字", "Name": "这里是软件安装后的名字", "Icon": "build/Icons/Icon256x256.png" } }, "nsis": { "oneClick": false, "perMachine": false, "allowToChangeInstallationDirectory": true, "installerIcon": "build/icons/icon.ico", "uninstallerIcon": "build/icons/icon.ico", "installerHeaderIcon": "build/icons/icon.ico", "createDesktopShortcut": true }, "deb": { "icon": "build/icons" }, "mac": { "icon": "build/icons/icon.icns", "identity": null }, "win": { "icon": "build/icons/icon.ico" }, "linux": { "icon": "build/icons", "target": ["AppImage", "deb"] } }
|
- 根据第二步打包命令执行即可
建议在当前平台只打包当前平台,如果打包其他平台可能会出现无法预知错误,可以利用虚拟机在一个电脑上打包多平台。在yarn build
时候你可能会遇到打包组件(electron fpm nsis)下载失败情况,请到这里下载对应版本到组件,然后放到系统缓存目录
1 2 3
| macOS ~/Library/Caches/electron-builder linux ~/.cache/electron-builder windows %LOCALAPPDATA%\electron-builder\cache
|
- 打包成功后在
build
目录里面会找到对应安装包
注意事项
- deb安装包在ubuntu安装成功后,会出现打不开软件情况,因为系统缺少
libgconf2-4
,运行下面命令安装即可
1
| sudo apt -y install libgconf2-4
|
软件更新
electron-builder
插件也自带更新功能,也可以把打包好的安装包发布到各个开源平台以及自定义。
注意事项
- 三个平台只支持特定的安装包格式,其他格式(deb)暂不支持
1 2 3
| macOS: DMG Linux: AppImage Windows: NSIS
|

- 添加发布配置
publish
配置添加到package.json
文件的build
下面会对所有平台生效,也可以为某个特定平台添加特有发布配置
1 2 3 4 5 6 7 8 9 10
| "publish": [ { "provider": "generic", "url": "http://127.0.0.1:2333/test/" } ]
|
- 相关代码
更新是写在主线程中,因为更新只能在打包后测试,为了调试方便建议使用electron-log
把每一步日志和错误都打印出来
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import { autoUpdater } from 'electron-updater' const log = require('electron-log')
ipcMain.on('check-update', (event, arg) => { log.info("check-update") autoUpdater.checkForUpdates() })
autoUpdater.on('error', (err) => { log.error("update-error") log.error(err) sendStatusToWindow(-1, '更新错误', 0) })
autoUpdater.on('checking-for-update', () => { log.info("checking-for-update") sendStatusToWindow(1, '正在检查更新', 0) })
autoUpdater.on('update-available', () => { log.info("update-available") sendStatusToWindow(1, '有可用更新', 0) })
autoUpdater.on('update-not-available', () => { log.info("update-not-available") sendStatusToWindow(1, '没有可用更新', 0) })
autoUpdater.on('download-progress', (progress, bytesPerSecond, percent, total, transferred) => { log.info("download-progress") log.info(parseInt(progress.percent) + '%') sendStatusToWindow(1, '下载中', progress.percent) })
autoUpdater.on('update-downloaded', (UpdateInfo) => { log.info('update-downloaded') log.info(UpdateInfo) sendStatusToWindow(2, '已下载完成', 100) ipcMain.on('update-install', (event, arg) => { log.info('update-install') autoUpdater.quitAndInstall() }) })
|
- 上传安装包和yml文件
在yarn build
完成后,如果你配置了publish
会额外生成一个yaml
文件,此文件记录当前安装包一些信息,以便检查更新进行对比来判断是否要更新。默认情况下win平台会生成latestyml
,macos平台latest-mac.yml
,linux平台latest-linux.yml
,把安装包和yml文件一起上传到发布服务器相应路径下

参考资料