一、下載Electron
克隆下載Electron:
鏈接: electron-quick-start
1.下載之后安裝Electron依賴(lài)
npm i -g electron@latest
npm安裝electron總失敗使用下面的安裝方式
npm install -g cnpm --registry=https://registry.npmmirror.com
cnpm install --save-dev electron
2.安裝打包運(yùn)行
cnpm install electron-packager --save-dev
二、修改下載的Electron項(xiàng)目
1.修改index.html文件
代碼如下(示例):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://127.0.0.1:12001">
</head>
<body>
<!-- 這里可以添加其他頁(yè)面內(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')
//嘗試使用絕對(duì)路徑來(lái)進(jìn)行
const indexPath = path.join(__dirname, 'dist', 'index.html');
//mainWindow.loadFile(indexPath)
mainWindow.loadFile('./dist/index.html'); // 打包的dist路徑 把vue打包成dist放到Electron項(xiàng)目的根目錄下
mainWindow.webContents.openDevTools() // 在這里打開(kāi)開(kāi)發(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項(xiàng)目
1.修改vite.config.js文件
打包后得路徑修改成為./,避免Electron打包exe后顯示空白
2.修改.env.production文件
修改生產(chǎn)環(huán)境配置,配置為后端得地址,http://127.0.0.1:8080/ 避免避免Electron打包exe后接口調(diào)用不通得問(wèn)題
3.修改auth.js文件
修改點(diǎn)擊登錄后一直轉(zhuǎn)圈,不跳轉(zhuǎn)到index頁(yè)面得問(wèn)題,把Cookie獲取方式修改成localStorage
4.修改router下得index.js文件
修改跳轉(zhuǎn)到其他頁(yè)面空白得問(wèn)題,路由跳轉(zhuǎn)得問(wèn)題,把history修改成hash
6.修改Navbar.vue文件
修改退出登錄后頁(yè)面空白得問(wèn)題,把location.href修改成router.push({ path: “/login”});
四、Electron打包
把vue項(xiàng)目打成dist的包,放到Electron項(xiàng)目的根目錄下。
然后在Electron 項(xiàng)目中執(zhí)行npm run package,進(jìn)行打包exe,打完包之后在根目錄下的release的文件夾中有打包好的exe文件。
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-820763.html
遇到問(wèn)題文章參考鏈接:
參考1: spring-boot+vue項(xiàng)目使用 electron打包exe桌面項(xiàng)目,桌面端出現(xiàn)報(bào)錯(cuò)Failed to load resource: net::ERR_FILE_NOT_FOUND(解決)
參考2: 使用electron打包exe出現(xiàn)報(bào)錯(cuò) /E:/prod-api/captchaImage:1 Failed to load resource: net::ERR_FILE_NOT_FOUND(若依)
參考3: 使用Electron來(lái)給若依系統(tǒng)打包成exe程序,出現(xiàn)登錄成功但是不跳轉(zhuǎn)頁(yè)面(已解決)
參考4: electron /electron-quick-start
參考5: 解決npm安裝electron總失敗的問(wèn)題
參考6: Electron的打包windows exe的方法文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-820763.html
到了這里,關(guān)于使用Electron打包vue文件變成exe應(yīng)用程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!