很多前端项目需要PC端,vue项目打包结合electron非常方便
vue项目及打包就不再多说,一般大家都会将打包好的文件指向dist文件目录,下面就以此条件进行解说
npm i -D electron@latest
npm i -D electron-packager
在打包vue后的dist文件夹下增加electron.js和package.json
打包后 通过快捷键就可以打开调试模式 ctrl + shift + l 或者启动就开启调试窗口 如果不需要就注释掉
解开这行注释 mainWindow.webContents.openDevTools()
electron.js
// Modules to control application life and create native browser window
const electron = require('electron')
const path = require('path')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const globalShortcut = electron.globalShortcut //快捷键
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
const Menu = electron.Menu
function createWindow () {
Menu.setApplicationMenu(null)
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1280,
height: 720
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
// 通过快捷键就可以打开调试模式 ctrl + shift + l
globalShortcut.register('CommandOrControl+Shift+L', () => {
let focusWin = BrowserWindow.getFocusedWindow()
focusWin && focusWin.toggleDevTools()
})
}
// 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', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// 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', function () {
// 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 (mainWindow === null) createWindow()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
dist/package.json
{
"name": "electron-app",
"version": "1.0.0",
"description": "A minimal Electron application",
"main": "electron.js",
"scripts": {
"start": "electron ."
},
"repository": "https://github.com/electron/electron-quick-start",
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^6.0.12"
}
}
vue项目根目录下的package.json添加打包客户端命令 重要的是electron_build这条命令
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron_build": "electron-packager ./dist/ --platform=win32 --arch=x64 --icon=./src/assets/favicon.ico --overwrite" // 打包客户端命令
},
执行打包客户端命令
npm run electron_build
其中–platform是配置打包成什么平台的安装文件,下面是可选的值
其中–arch是指定系统是什么架构的,常见的例如32位和64位操作系统,这个参数的可选值有
参数–platform和–arch已经被标志为过期,新的写法如下
参考资料:
electron vue项目打包成桌面客户端
Mac 安装electron缓慢的3种解决方案https://blog.csdn.net/simplehouse/article/details/95594955
Electron 打包问题:electron-builder 下载各种依赖出错https://blog.csdn.net/cctvcqupt/article/details/87904368
electron入坑指南https://www.cnblogs.com/xueyoucd/p/8006610.html
electron淘宝镜像地址https://npm.taobao.org/mirrors/electron/
【vue.config.js打包配置】
全局-cli-配置
vue.config.js 配置