前端vue项目打包成桌面端exe应用

主要 使用 Electron将 vue项目打包为 exe

1.首先下载Electron

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

安装完依赖之后

npm start

运行成功

npm start

注意:如果你的项目使用了VueRouter,那么切记:VueRouter一定不能是History模式

2.在electron-quick-start文件中安装打包需要的依赖。

npm install electron-packager --save-dev

3.在 electron-quick-start 项目中 找到 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({
    resizable: true, //是否支持调整窗口大小
    icon: './dist/favicon.ico', //	左上角图标
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
  });

  // mainWindow.setMenu(null); //	隐藏顶部菜单栏
  // and load the index.html of the app.
  mainWindow.loadFile('./dist/index.html');
  // Open the DevTools.
  mainWindow.webContents.openDevTools();
  // //	默认窗口最大化
  // mainWindow.maximize();
  // mainWindow.show();
}

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

4.在 electron-quick-start 项目 package.json 配置文件中,scripts 下添加 packager 指令(icon图标,也可以不设置)

  "scripts": {
    "start": "electron .",
    "packager": "electron-packager ./ HumeErp --platform=win32 --icon=./dist/favicon.ico --arch=x64 --overwrite"
  },

5.打包原 Vue 项目,将打包后生成的 dist 文件夹放在 electron-quick-start 项目中与node_modules 平级即可

在这里插入图片描述

6.输入打包命令 npm run packager 执行成功后,electron-quick-start 项目中会出现一个 App-win32-x64 的文件夹,该文件夹内 App.exe 即为项目的启动文件

在这里插入图片描述
在这里插入图片描述

### 将前端项目打包为可执行文件的方法 #### 使用 Electron 打包前端项目 可以通过 **Electron** 框架将基于 HTML、CSS、JavaScript 和 jQuery 的前端项目打包一个跨平台的桌面应用程序,并最终生 `.exe` 文件。以下是实现这一目标的关键步骤: 1. 安装 Node.js 和 npm,因为它们是运行 Electron 应用的基础环境[^1]。 2. 初始化一个新的 Node.js 项目并安装 Electron: ```bash npm init -y npm install electron --save-dev ``` 3. 创建 `main.js` 文件作为 Electron 主进程入口,并配置 `package.json` 中的启动脚本: ```json { "name": "your-app-name", "version": "1.0.0", "main": "main.js", "scripts": { "start": "electron ." } } ``` 4. 编写 `main.js` 来加载您的前端页面: ```javascript const { app, BrowserWindow } = require('electron'); function createWindow() { let win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }); win.loadFile('index.html'); // 加载您的前端页面 } app.whenReady().then(createWindow); ``` 5. 使用 `electron-packager` 工具来创建独立的应用程序包: ```bash npx electron-packager ./ YourAppName --platform=win32 --arch=x64 --out=out/ ``` 这会生适用于 Windows 平台的可执行文件。 为了进一步增强功能,还可以使用其他工具完更复杂的任务,例如制作安装程序或自动生快捷方式[^2]。 --- #### 使用 Nativefier 快速打包网页为 EXE 文件 如果仅需将某个网站封装为本地桌面应用,则可以考虑使用 **Nativefier** 工具。它是一个轻量级解决方案,能够快速将任何 URL 转换为原生桌面应用程序。 1. 全局安装 Nativefier: ```bash npm install nativefier -g ``` 2. 使用命令行指定目标网址和输出路径: ```bash nativefier https://example.com/ --name="ExampleApp" --platform=win32 --arch=x64 ``` 这一步骤将会生一个包含 `.exe` 可执行文件在内的完整桌面应用目录结构[^3]。 --- #### 总结 对于复杂前端项目打包需求,推荐采用 **Electron** 方案以获得更高的灵活性与定制能力;而对于简单场景下的单页应用或者特定网站封装,可以选择更为便捷高效的 **Nativefier** 方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值