使用electron把vue项目打包成windows应用exe程序

本文介绍了如何将Vue项目与Electron结合,创建一个桌面应用程序。首先,通过npm安装必要的Electron和Electron-packager依赖。接着,修改vue.config.js文件设置publicPath。然后,执行npm run build生成dist文件夹,并在该目录下创建electron.js和package.json。在package.json中添加新的脚本以进行Electron打包。最后,运行npm run electron_build完成打包过程,生成的exe文件可以直接运行。这个教程适合想要将Vue应用转化为桌面应用的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、安装electron依赖

npm install electron --save-dev
 
npm install electron-packager --save-dev

2、修改vue.config.js文件

const publicPath = process.env.VUE_APP_PUBLIC_PATH || './'

3、打包生成dist文件夹

npm run build

4、在dist文件夹下创建electron.js和package.json文件

electron.js

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1200,
    height: 900,
    webPreferences: {
      preload: path.join(__dirname, 'index.html')
    }
  })
  // and load the index.html of the app.
  mainWindow.loadFile(__dirname+'/index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// 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.whenReady().then(() => {
  createWindow()

  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 (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

// 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.

package.json

{
  "name": "CTS",
  "version": "1.0.0",
  "description": "测试app",
  "main": "electron.js",
  "scripts": {
    "start": "electron "
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "ysx",
  "license": "CC0-1.0",
  "devDependencies": {
    "electron": "^12.0.5"
  }
}

5、在package.json新增启动

"scripts": {
    "serve": "vue-cli-service serve --open",
    "start": "npm run serve",
    "dev": "npm run serve",
    "build": "vue-cli-service build",
    "build:preview": "NODE_OPTIONS=--max_old_space_size=4096 vue-cli-service build --mode preview",
    "lint": "vue-cli-service lint --fix",
    "test:unit": "vue-cli-service test:unit",
    "electron_build": "electron-packager dist --platform=win32 --arch=x64 --icon=public/icon.ico --overwrite"//新增的
  },

6、开始打包

npm run electron_build

7、最终生成一个文件夹,双击exe文件可运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值