点个star在看
自动集成
安装


选择electron版本,这里先选v11,后面改为v13
看一下变化
package.json

多了一些依赖与启动命令
.gitinore

多了一个electron输出文件夹
background.ts

多了这么个主进程文件
"use strict";import { app, protocol, BrowserWindow } from "electron";import { createProtocol } from "vue-cli-plugin-electron-builder/lib";import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";const isDevelopment = process.env.NODE_ENV !== "production";// Scheme must be registered before the app is readyprotocol.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 infonodeIntegration: (process.env.ELECTRON_NODE_INTEGRATION as unknown) as boolean,},});if (process.env.WEBPACK_DEV_SERVER_URL) {// Load the url of the dev server if in development modeawait win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);if (!process.env.IS_TEST) win.webContents.openDevTools();} else {createProtocol("app");// Load the index.html when not in developmentwin.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 + Qif (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 Devtoolstry {await installExtension(VUEJS_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();});}}
优缺点
优点
缺点
不能自定义,且文件位置不可修改,不够灵活
手动集成
修改package.json

{"name": "veet","description": "vue3+electron+element-plus+ts手把手搭建教程","version": "0.1.2","homepage": "https://github.com/sibaiabis/veet","author": {"name": "siBai","url": "https://github.com/sibaiabis"},"private": true,"scripts": {"serve": "vue-cli-service serve","build": "vue-cli-service build","lint": "vue-cli-service lint","electron:build": "vue-cli-service electron:build","electron:serve": "vue-cli-service electron:serve"},"mainComment": "app.asar There must be a main entry. In fact, the app.asar Decompress and you'll find that there are background.js So our package.json To set the entry in, that is, main: background.js","main": "background.js","dependencies": {"core-js": "^3.10.2","element-plus": "^1.0.2-beta.40","vue": "^3.0.11","vue-router": "^4.0.6","vuex": "^4.0.0"},"devDependencies": {"@types/electron-devtools-installer": "^2.2.0","@typescript-eslint/eslint-plugin": "^4.22.0","@typescript-eslint/parser": "^4.22.0","@vue/cli-plugin-babel": "^5.0.0-alpha.8","@vue/cli-plugin-eslint": "^5.0.0-alpha.8","@vue/cli-plugin-router": "^5.0.0-alpha.8","@vue/cli-plugin-typescript": "^5.0.0-alpha.8","@vue/cli-plugin-vuex": "^5.0.0-alpha.8","@vue/cli-service": "^5.0.0-alpha.8","@vue/compiler-sfc": "^3.0.11","@vue/eslint-config-prettier": "^6.0.0","@vue/eslint-config-typescript": "^7.0.0","electron": "^13.0.0-beta.14","electron-devtools-installer": "^3.2.0","eslint": "^7.24.0","eslint-plugin-prettier": "^3.4.0","eslint-plugin-vue": "^7.9.0","prettier": "^2.2.1","sass": "^1.32.11","sass-loader": "^11.0.1","typescript": "^4.2.4","vue-cli-plugin-electron-builder": "^2.0.0-rc.6"}}
说明:
“main”: “background.js”, 不可修改,给我镶死这里!!!
“mainComment“: “xxxx“, 可以不加,非必须。这是一种在package.json里加注释的方式,俗称siBai式
修改.gitignore

yarn.lock/veet_electron/veet_dist
新增.yarnrc

registry "https://registry.npm.taobao.org"sass_binary_site "https://npm.taobao.org/mirrors/node-sass/"phantomjs_cdnurl "http://cnpmjs.org/downloads"electron_mirror "https://npm.taobao.org/mirrors/electron/"sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/"profiler_binary_host_mirror "https://npm.taobao.org/mirrors/node-inspector/"chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver"
新增main.ts

"use strict";import { app, protocol, BrowserWindow } from "electron";import { createProtocol } from "vue-cli-plugin-electron-builder/lib";import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";const isDevelopment = process.env.NODE_ENV !== "production";// Scheme must be registered before the app is readyprotocol.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 infonodeIntegration: (process.env.ELECTRON_NODE_INTEGRATION as unknown) as boolean,},});if (process.env.WEBPACK_DEV_SERVER_URL) {// Load the url of the dev server if in development modeawait win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);if (!process.env.IS_TEST) win.webContents.openDevTools();} else {createProtocol("app");// Load the index.html when not in developmentwin.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 + Qif (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 Devtoolstry {await installExtension(VUEJS_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();});}}
新增vue.config.js

module.exports = {pluginOptions: {electronBuilder: {mainProcessFile: 'electron/main.ts',outputDir: 'veet_electron',nodeIntegration: true,builderOptions: {appId: 'cn.siBai.veet',productName: 'veet',copyright: 'Copyright © 2021 siBai',directories: {output: './veet_dist',},win: {icon: './electron/ico/install 256x256.ico',target: [{target: 'nsis',arch: ['x64', 'ia32'],},],},nsis: {// https://www.jianshu.com/p/1701baa240fdoneClick: false,perMachine: true,allowElevation: true,allowToChangeInstallationDirectory: true,installerIcon: './electron/ico/install 256x256.ico',uninstallerIcon: './electron/ico/uninstall 256x256.ico',installerHeaderIcon: './electron/ico/install 256x256.ico',createDesktopShortcut: true,createStartMenuShortcut: true,shortcutName: 'veet',},},},},}
优缺点
优点
缺点
不适合初学者,配置起来比较麻烦一些
启动查看
查看

棒!
打包构建
构建产物
安装
启动

