主要 使用 Electron將 vue項目打包為 exe
1.首先下載Electron
git clone https://github.com/electron/electron-quick-start
cd electron-quick-start
npm install
安裝完依賴之后
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 即為項目的啟動文件