使用Electron打包vue文件變成exe應(yīng)用程序的全過程
一、下載Electron
克隆下載Electron:
鏈接: electron-quick-start
1.下載之后安裝Electron依賴
npm i -g electron@latest
npm安裝electron總失敗使用下面的安裝方式
npm install -g cnpm --registry=https://registry.npmmirror.com
cnpm install --save-dev electron
2.安裝打包運行
cnpm install electron-packager --save-dev
二、修改下載的Electron項目

1.修改index.html文件
代碼如下(示例):
<!DOCTYPE html> <html> <head> <meta http-equiv="refresh" content="0;url=http://127.0.0.1:12001"> </head> <body> <!-- 這里可以添加其他頁面內(nèi)容 --> </body> </html>
2.修改main.js文件
代碼如下(示例):
// Modules to control application life and create native browser window
const { app, BrowserWindow,Menu, shell } = require('electron')
const path = require('path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1200,
height: 600,
webPreferences: {
nodeIntegration: true,//取消新增
contextIsolation: false,//取消新增路徑
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
// mainWindow.loadFile('./dist/index.html')
//嘗試使用絕對路徑來進行
const indexPath = path.join(__dirname, 'dist', 'index.html');
//mainWindow.loadFile(indexPath)
mainWindow.loadFile('./dist/index.html'); // 打包的dist路徑 把vue打包成dist放到Electron項目的根目錄下
mainWindow.webContents.openDevTools() // 在這里打開開發(fā)者工具
// 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.
3.修改package.json文件
代碼如下(示例):
{
"name": "electron-quick-start",
"version": "1.0.0",
"description": "A minimal Electron application",
"main": "main.js",
"scripts": {
"package": "electron-packager ./ aa --platform=win32 --out=release --arch=x64 --overwrite --download.mirrorOptions.mirror=https://npm.taobao.org/mirrors/electron/ --ignore=node_modules"
},// aa是生成的文件夾和exe的名字可修改名字
"repository": "https://github.com/electron/electron-quick-start",
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^28.1.3",
"electron-packager": "^17.1.2"
}
}
三、修改vue項目
1.修改vite.config.js文件
打包后得路徑修改成為./,避免Electron打包exe后顯示空白

2.修改.env.production文件
修改生產(chǎn)環(huán)境配置,配置為后端得地址,http://127.0.0.1:8080/ 避免避免Electron打包exe后接口調(diào)用不通得問題

3.修改auth.js文件
修改點擊登錄后一直轉(zhuǎn)圈,不跳轉(zhuǎn)到index頁面得問題,把Cookie獲取方式修改成localStorage

4.修改router下得index.js文件
修改跳轉(zhuǎn)到其他頁面空白得問題,路由跳轉(zhuǎn)得問題,把history修改成hash

5.修改Navbar.vue文件
修改退出登錄后頁面空白得問題,把location.href修改成router.push({ path: “/login”});

四、Electron打包
把vue項目打成dist的包,放到Electron項目的根目錄下。

然后在Electron 項目中執(zhí)行npm run package,進行打包exe,打完包之后在根目錄下的release的文件夾中有打包好的exe文件。

以上就是使用Electron打包vue文件變成exe應(yīng)用程序的全過程的詳細內(nèi)容,更多關(guān)于Electron打包vue文件成exe的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue el-table 動態(tài)添加行與刪除行的實現(xiàn)
這篇文章主要介紹了vue el-table 動態(tài)添加行與刪除行的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
關(guān)于vue利用postcss-pxtorem進行移動端適配的問題
這篇文章主要介紹了關(guān)于vue利用postcss-pxtorem進行移動端適配的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
reactive readonly嵌套對象轉(zhuǎn)換功能實現(xiàn)詳解
這篇文章主要為大家介紹了reactive readonly嵌套對象轉(zhuǎn)換功能實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
el-menu如何根據(jù)多層樹形結(jié)構(gòu)遞歸遍歷展示菜單欄
這篇文章主要介紹了el-menu根據(jù)多層樹形結(jié)構(gòu)遞歸遍歷展示菜單欄,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-07-07

