Electron - vue项目打包成.exe文件(通过官方demo的方式)

一、拉取官方demo (electron-quick-start)

//将electron官网中的 electron-quick-start 拉取到本地 

git clone https://github.com/electron/electron-quick-start

 无法访问到github可以直接下载压缩包:

链接: https://pan.baidu.com/s/155m-g5sJ11lsCqwC5_F9oQ?pwd=kbsy 提取码: kbsy

1、成功拉取或下载zip包后目录如下:

 2、安装好后,用vscode等开发工具打开,新建终端,输入下面的命令。

下载项目依赖

npm install
或者
npm i

下载打包所需依赖

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

package.json 启动命令是自动新增的

 

npm run start 启动项目

启动成功显示 Hello World!

二、项目打包 

注意:

1.前端路由模式更改为hash模式

2.vue.config.js 中publicPath设为 ./

3.vite项目需要将base 设为 ./

将vue项目打包(npm run build), 会生成一个dist文件

三、修改electron-quick-start 文件

1.删除 electron-quick-start 文件中的index.html,把vue项目打包生成的dist文件放到目录下
2.打开 electron-quick-start 文件里的main.js文件,附上mian.js完整代码
3.重点是:mainWindow.loadFile(“./dist/index.html”)  引用入口文件的路径修改

 

附带main.js文件内容:

// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('node:path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('./dist/index.html') // 入口文件引入
  // Close the Menu.
  mainWindow.setMenu(null) // 去掉顶部菜单栏
  // 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.

 再次执行 ( npm run start ) 启动页面

 electron-quick-start 项目 package.json 配置文件中,scripts 下添加 packager 打包指令

  "scripts": {
    "start": "electron .",
    "packager": "electron-packager ./ 测试xxx系统 --platform=win32 --arch=x64 --icon=./dist/favicon.ico --out=./out --overwrite"
  }, // 测试xxx系统 为应用名 icon 为图标路径 可自行修改
 npm run packager 执行打包指令

打包完毕, main.js 同级 会生成out文件, exe包就是打包好的文件 , 可安装在windows系统上

至此 exe 打包已完成!!!

四、使用 Inno Setup(工具生成安装程序包)

在Inno Setup 里面搭建一个脚本, 通过脚本编辑安装程序的名称, 版本. 快捷键生成, 安装目录等, 是很有必要的, 生成一次后,后续可以一直沿用 ,二次使用选择上一次创建好的脚本, 一键打包即可

打包安装程序安装包:

链接: https://pan.baidu.com/s/1Kd3QK7eQB1IPOeb9DrPMPQ?pwd=3kbb 提取码: 3kbb

按照安装流程来, 基本没问题

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

以上已经可以把exe打包成安装程序 

打包成功的exe安装程序 在Output文件夹下,点击即可安装

exe打包、安装到此结束  !!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值