搜索会员中心 创作中心Vue2项目一键打包成桌面应用

1、 安装Electron

在Vue项目 目录下,执行以下命令安装Electron:
npm install electron --save-dev

2、 创建Electron主进程

在Vue项目根目录下创建一个名为main .js的文件,该文件将作为Electron的主进程

javascript 复制代码
const { app, BrowserWindow, Menu } = require('electron')
const path = require('path')

Menu.setApplicationMenu(null)
function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      sandbox: true,
      preload: path.join(__dirname, 'preload.js')
    },
    menu: null,

    // 🔥 双重保险:彻底关闭菜单
    autoHideMenuBar: true,
    frame: true, // 正常窗口(不想有标题栏可以改成 false)
    // 👇 最关键配置
    icon: path.join(__dirname, 'logo.ico')
  })
  win.setMenu(null)
  win.menuBarVisible = false
  win.loadFile(path.join(__dirname, 'dist/index.html'))
}

app.whenReady().then(() => {
  createWindow()
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

3. 配置打包参数

在项目 根目录下创建一个名为electron-builder.json的文件,用于配置打包参数

javascript 复制代码
{
  "productName": "cdl", // 安装之后软件的名称
  "appId": "myapp",
  "directories": {
    "output": "release"
  },
  "files": [
    "dist/**/*",
    "main.js"
  ],
  "win": {
    "target": "nsis",
    "icon": "src/assets/icon.ico"
  }
}

4. 执行打包命令

在项目根目录下执行以下命令进行打包:
npm run electron:build

其他配置

background.js

javascript 复制代码
'use strict'

import { app, protocol, BrowserWindow, Menu } 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'
Menu.setApplicationMenu(null)

// 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')
  }

  // 创建窗口时加入
  win.setMenu(null)
  win.menuBarVisible = false
}

// 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(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()
    })
  }
}

如果想要修改打开软件之后右上角显示的名字

相关推荐
excel10 小时前
从封装到继承:深入理解 TypeScript 类中的 public、private、protected、static
前端
imkaifan10 小时前
工作流(Worker/Graph)配置对象如何解读、子图
javascript·工作流·(worker/graph)·配置对象如何解读·子图
向日的葵00610 小时前
vue3路由的replace属性(四)
前端·javascript·vue.js·vue路由
杨超越luckly10 小时前
Agent应用指南:利用GET请求获取理想汽车门店位置信息
前端·python·html·汽车·可视化
阿猫的故乡10 小时前
Vue模板引用和组件暴露:ref拿DOM、defineExpose调方法,案例多到眼花
前端·javascript·vue.js
薛定谔的猫-菜鸟程序员10 小时前
从Electron到Tauri,Rust+Vue(Tauri) 实现超高性能桌面日志应用开发,以及开发避坑指南
vue.js·rust·electron
小雨下雨的雨15 小时前
井字棋AI机器人实现详解 - Minimax算法实战-鸿蒙PC Electron框架完成
前端·人工智能·算法·华为·electron·鸿蒙
ZC跨境爬虫19 小时前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript
fangdengfu12319 小时前
ES分析系统各个服务日志占用量
java·前端·elasticsearch
凌云拓界19 小时前
文件管理:让AI安全操作你的电脑 ——CogitoAgent开发实战(三)
javascript·人工智能·架构·开源·node.js