1. 创建 vue 项目
如果没有安装vue-cli,需要先安装一下
npm install -g @vue/cli // npm 安装
yarn global add @vue/cli // yarn 安装
创建vue项目
vue create vue-electron
2. 集成electron
首先要设置代理
yarn config set ELECTRON_MIRROR http://npm.taobao.org/mirrors/electron/
然后,安装electron、vue-cli-plugin-electron-builder以及electron-devtools-installer,后两个插件用于打包。
// 安装electron
yarn add electron --dev
// 安装electron打包插件
yarn add vue-cli-plugin-electron-builder --dev
yarn add electron-devtools-installer --dev
3.修改配置
1.src目录下新建一个background.js文件,作为electron的入口文件
'use strict'
import { app, protocol, BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS3_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])
async function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
}
})
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
createProtocol('app')
// Load the index.html when not in development
win.loadURL('app://./index.html')
}
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installExtension(VUEJS3_DEVTOOLS)
} catch (e) {
console.error('Vue Devtools failed to install:', e.toString())
}
}
createWindow()
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
})
}
}
2.修改package.json文件,新增入口属性和运行命令
这里main,author,description这三项都要填写
{
...
"main": "background.js", // electron入口文件
"author": "hdat/ 作者
"description": "测试"
...
"scripts": {
...
"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps"
}
}
3.修改vue.config.js文件,新增electron打包选项
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
publicPath: './', // 这里设置成./,否则打包后vue的静态资源引用会报错找不到路径导致白屏
pluginOptions: {
electronBuilder: {
builderOptions: {
"appId": "xxxxxx", // 程序的包ID
"productName": "测试
"copyright": "Copyright © 2023dat
"directories": {
"output": "./dist"//输出文件路径
},
"win": {//win相关配置
"icon": "./public/logo.ico",//图标,这里图标需要至少256*256,否则打包会报错
"target": [
{
"target": "nsis",//利用nsis制作安装程序
"arch": [
"x64",//64位
"ia32"//32位
]
}
]
},
"nsis": {
"oneClick": false, // 是否一键安装
"allowElevation": true, // 允许请求提升。 如果为false,则用户必须使用提升的权限重新启动安装程序。
"allowToChangeInstallationDirectory": true, // 允许修改安装目录
"installerIcon": "./public/logo.ico",// 安装图标
"uninstallerIcon": "./public/logo.ico",//卸载图标
"installerHeaderIcon": "./public/logo.ico", // 安装时头部图标
"createDesktopShortcut": true, // 创建桌面图标
"createStartMenuShortcut": true,// 创建开始菜单图标
"shortcutName": "taskmanage", // 图标名称
},
}
}
}
})
4.运行和打包
运行没什么可说的,主要是打包,打包过程中他会从github下载代码,那个你不用管他,让他自己慢慢下载就好了。
效果