点个star在看


官网

自动集成

安装

elecAuto.pngelecAuto2.png

选择electron版本,这里先选v11,后面改为v13

看一下变化

package.json

elecp.png

多了一些依赖与启动命令

.gitinore

elece.png

多了一个electron输出文件夹

background.ts

elecb.png

多了这么个主进程文件

  1. "use strict";
  2. import { app, protocol, BrowserWindow } from "electron";
  3. import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
  4. import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
  5. const isDevelopment = process.env.NODE_ENV !== "production";
  6. // Scheme must be registered before the app is ready
  7. protocol.registerSchemesAsPrivileged([
  8. { scheme: "app", privileges: { secure: true, standard: true } },
  9. ]);
  10. async function createWindow() {
  11. // Create the browser window.
  12. const win = new BrowserWindow({
  13. width: 800,
  14. height: 600,
  15. webPreferences: {
  16. // Use pluginOptions.nodeIntegration, leave this alone
  17. // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
  18. nodeIntegration: (process.env.ELECTRON_NODE_INTEGRATION as unknown) as boolean,
  19. },
  20. });
  21. if (process.env.WEBPACK_DEV_SERVER_URL) {
  22. // Load the url of the dev server if in development mode
  23. await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);
  24. if (!process.env.IS_TEST) win.webContents.openDevTools();
  25. } else {
  26. createProtocol("app");
  27. // Load the index.html when not in development
  28. win.loadURL("app://./index.html");
  29. }
  30. }
  31. // Quit when all windows are closed.
  32. app.on("window-all-closed", () => {
  33. // On macOS it is common for applications and their menu bar
  34. // to stay active until the user quits explicitly with Cmd + Q
  35. if (process.platform !== "darwin") {
  36. app.quit();
  37. }
  38. });
  39. app.on("activate", () => {
  40. // On macOS it's common to re-create a window in the app when the
  41. // dock icon is clicked and there are no other windows open.
  42. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  43. });
  44. // This method will be called when Electron has finished
  45. // initialization and is ready to create browser windows.
  46. // Some APIs can only be used after this event occurs.
  47. app.on("ready", async () => {
  48. if (isDevelopment && !process.env.IS_TEST) {
  49. // Install Vue Devtools
  50. try {
  51. await installExtension(VUEJS_DEVTOOLS);
  52. } catch (e) {
  53. console.error("Vue Devtools failed to install:", e.toString());
  54. }
  55. }
  56. createWindow();
  57. });
  58. // Exit cleanly on request from parent process in development mode.
  59. if (isDevelopment) {
  60. if (process.platform === "win32") {
  61. process.on("message", (data) => {
  62. if (data === "graceful-exit") {
  63. app.quit();
  64. }
  65. });
  66. } else {
  67. process.on("SIGTERM", () => {
  68. app.quit();
  69. });
  70. }
  71. }

优缺点

优点

省事

缺点

不能自定义,且文件位置不可修改,不够灵活


手动集成

这里使用electron v13.0.0

修改package.json

elecSp.png

  1. {
  2. "name": "veet",
  3. "description": "vue3+electron+element-plus+ts手把手搭建教程",
  4. "version": "0.1.2",
  5. "homepage": "https://github.com/sibaiabis/veet",
  6. "author": {
  7. "name": "siBai",
  8. "url": "https://github.com/sibaiabis"
  9. },
  10. "private": true,
  11. "scripts": {
  12. "serve": "vue-cli-service serve",
  13. "build": "vue-cli-service build",
  14. "lint": "vue-cli-service lint",
  15. "electron:build": "vue-cli-service electron:build",
  16. "electron:serve": "vue-cli-service electron:serve"
  17. },
  18. "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",
  19. "main": "background.js",
  20. "dependencies": {
  21. "core-js": "^3.10.2",
  22. "element-plus": "^1.0.2-beta.40",
  23. "vue": "^3.0.11",
  24. "vue-router": "^4.0.6",
  25. "vuex": "^4.0.0"
  26. },
  27. "devDependencies": {
  28. "@types/electron-devtools-installer": "^2.2.0",
  29. "@typescript-eslint/eslint-plugin": "^4.22.0",
  30. "@typescript-eslint/parser": "^4.22.0",
  31. "@vue/cli-plugin-babel": "^5.0.0-alpha.8",
  32. "@vue/cli-plugin-eslint": "^5.0.0-alpha.8",
  33. "@vue/cli-plugin-router": "^5.0.0-alpha.8",
  34. "@vue/cli-plugin-typescript": "^5.0.0-alpha.8",
  35. "@vue/cli-plugin-vuex": "^5.0.0-alpha.8",
  36. "@vue/cli-service": "^5.0.0-alpha.8",
  37. "@vue/compiler-sfc": "^3.0.11",
  38. "@vue/eslint-config-prettier": "^6.0.0",
  39. "@vue/eslint-config-typescript": "^7.0.0",
  40. "electron": "^13.0.0-beta.14",
  41. "electron-devtools-installer": "^3.2.0",
  42. "eslint": "^7.24.0",
  43. "eslint-plugin-prettier": "^3.4.0",
  44. "eslint-plugin-vue": "^7.9.0",
  45. "prettier": "^2.2.1",
  46. "sass": "^1.32.11",
  47. "sass-loader": "^11.0.1",
  48. "typescript": "^4.2.4",
  49. "vue-cli-plugin-electron-builder": "^2.0.0-rc.6"
  50. }
  51. }

说明:
“main”: “background.js”, 不可修改,给我镶死这里!!!
mainComment“: “xxxx“, 可以不加,非必须。这是一种在package.json里加注释的方式,俗称siBai式

修改.gitignore

elecGit.png

  1. yarn.lock
  2. /veet_electron
  3. /veet_dist

新增.yarnrc

elecYarnrc.png

  1. registry "https://registry.npm.taobao.org"
  2. sass_binary_site "https://npm.taobao.org/mirrors/node-sass/"
  3. phantomjs_cdnurl "http://cnpmjs.org/downloads"
  4. electron_mirror "https://npm.taobao.org/mirrors/electron/"
  5. sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/"
  6. profiler_binary_host_mirror "https://npm.taobao.org/mirrors/node-inspector/"
  7. chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver"

新增main.ts

elecMain.png

  1. "use strict";
  2. import { app, protocol, BrowserWindow } from "electron";
  3. import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
  4. import installExtension, { VUEJS_DEVTOOLS } from "electron-devtools-installer";
  5. const isDevelopment = process.env.NODE_ENV !== "production";
  6. // Scheme must be registered before the app is ready
  7. protocol.registerSchemesAsPrivileged([
  8. { scheme: "app", privileges: { secure: true, standard: true } },
  9. ]);
  10. async function createWindow() {
  11. // Create the browser window.
  12. const win = new BrowserWindow({
  13. width: 800,
  14. height: 600,
  15. webPreferences: {
  16. // Use pluginOptions.nodeIntegration, leave this alone
  17. // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
  18. nodeIntegration: (process.env.ELECTRON_NODE_INTEGRATION as unknown) as boolean,
  19. },
  20. });
  21. if (process.env.WEBPACK_DEV_SERVER_URL) {
  22. // Load the url of the dev server if in development mode
  23. await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);
  24. if (!process.env.IS_TEST) win.webContents.openDevTools();
  25. } else {
  26. createProtocol("app");
  27. // Load the index.html when not in development
  28. win.loadURL("app://./index.html");
  29. }
  30. }
  31. // Quit when all windows are closed.
  32. app.on("window-all-closed", () => {
  33. // On macOS it is common for applications and their menu bar
  34. // to stay active until the user quits explicitly with Cmd + Q
  35. if (process.platform !== "darwin") {
  36. app.quit();
  37. }
  38. });
  39. app.on("activate", () => {
  40. // On macOS it's common to re-create a window in the app when the
  41. // dock icon is clicked and there are no other windows open.
  42. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  43. });
  44. // This method will be called when Electron has finished
  45. // initialization and is ready to create browser windows.
  46. // Some APIs can only be used after this event occurs.
  47. app.on("ready", async () => {
  48. if (isDevelopment && !process.env.IS_TEST) {
  49. // Install Vue Devtools
  50. try {
  51. await installExtension(VUEJS_DEVTOOLS);
  52. } catch (e) {
  53. console.error("Vue Devtools failed to install:", e.toString());
  54. }
  55. }
  56. createWindow();
  57. });
  58. // Exit cleanly on request from parent process in development mode.
  59. if (isDevelopment) {
  60. if (process.platform === "win32") {
  61. process.on("message", (data) => {
  62. if (data === "graceful-exit") {
  63. app.quit();
  64. }
  65. });
  66. } else {
  67. process.on("SIGTERM", () => {
  68. app.quit();
  69. });
  70. }
  71. }

新增vue.config.js

elecConfig.png

  1. module.exports = {
  2. pluginOptions: {
  3. electronBuilder: {
  4. mainProcessFile: 'electron/main.ts',
  5. outputDir: 'veet_electron',
  6. nodeIntegration: true,
  7. builderOptions: {
  8. appId: 'cn.siBai.veet',
  9. productName: 'veet',
  10. copyright: 'Copyright © 2021 siBai',
  11. directories: {
  12. output: './veet_dist',
  13. },
  14. win: {
  15. icon: './electron/ico/install 256x256.ico',
  16. target: [
  17. {
  18. target: 'nsis',
  19. arch: ['x64', 'ia32'],
  20. },
  21. ],
  22. },
  23. nsis: {
  24. // https://www.jianshu.com/p/1701baa240fd
  25. oneClick: false,
  26. perMachine: true,
  27. allowElevation: true,
  28. allowToChangeInstallationDirectory: true,
  29. installerIcon: './electron/ico/install 256x256.ico',
  30. uninstallerIcon: './electron/ico/uninstall 256x256.ico',
  31. installerHeaderIcon: './electron/ico/install 256x256.ico',
  32. createDesktopShortcut: true,
  33. createStartMenuShortcut: true,
  34. shortcutName: 'veet',
  35. },
  36. },
  37. },
  38. },
  39. }

优缺点

优点

完全自定义呐

缺点

不适合初学者,配置起来比较麻烦一些


启动查看

查看

elecSuccess.png
棒!


打包构建

构建产物

elecBuild.png

安装

elecAdd.png

启动

elecSuccess2.png