vue3使用Electron打包成exe的方法與打包報(bào)錯(cuò)解決
一、安裝
1.安裝electron
npm install electron --save-dev npm install electron-builder --save-dev
2.在vue項(xiàng)目根目錄新建文件index.js
// main.js // Modules to control application life and create native browser window const { app, BrowserWindow,Menu } = require('electron') const createWindow = () => { // Create the browser window. const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, // 啟用 Node.js 集成 contextIsolation: false, // 禁用上下文隔離(Node.js 集成的一個(gè)選項(xiàng)) webSecurity: false, // 禁用同源策略 contextIsolation: false, session: { cookies: true // 這行確保啟用了cookie } }, icon: 'build/.icon-ico/icon.ico'//這里是自動(dòng)生成的圖標(biāo),默認(rèn)情況下不需要改 }) // and load the index.html of the app. mainWindow.loadFile('./dist/index.html')//如果要本地化,這樣寫(xiě),如果寫(xiě)遠(yuǎn)程的,那這里就是請(qǐng)求的域名 //隱藏頂部菜單 // mainWindow.setMenu(null); // Open the DevTools. // Open the DevTools. //mainWindow.webContents.openDevTools() mainWindow.maximize();//默認(rèn)最大化 } //設(shè)置中文菜單,默認(rèn)是英文的 const createMenu = () => { const template = [ { label: '文件', submenu: [ { label: '退出', accelerator: 'CmdOrCtrl+Q', click: () => { app.quit(); } } ] }, { label: '查看', submenu: [ { label: '重新加載', accelerator: 'F5', role: 'reload' }, { label: '全屏', accelerator: 'F11', role: 'togglefullscreen' }, { label: '開(kāi)發(fā)者工具', role: 'toggledevtools' } ] } ]; const menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu); } // 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() createMenu()//菜單設(shè)置 app.on('activate', () => { // 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', () => { 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文件編輯
"scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "lint": "vue-cli-service lint", "electron:build": "vue-cli-service build && electron-builder", "electron:serve": "electron ." }, "build": { "productName": "這里是你的項(xiàng)目名", "appId": "com.example.這里是appId", "win": { "icon": "favicon.ico", "target": ["nsis", "appx"] }, "directories": { "output": "build" }, "files": [ "dist/**/*", "index.js"http://這里是剛才建的index.js ] },
4.測(cè)試
npm run electron:serve
5.打包
npm run electron:build
二、報(bào)錯(cuò)解決
解決:打開(kāi)cmd 執(zhí)行 npm config edit
npm config edit
打開(kāi)配置文件 粘貼以下內(nèi)容
registry=https://registry.npm.taobao.org/ sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ phantomjs_cdnurl=http://npm.taobao.org/mirrors/phantomjs electron_mirror=https://npm.taobao.org/mirrors/electron/ ELECTRON_MIRROR=http://npm.taobao.org/mirrors/electron/
三、使用
真正用的時(shí)候會(huì)出現(xiàn)各種,比例啟動(dòng)exe的圖標(biāo),cookie。
還有就是,平時(shí)我的打包項(xiàng)目不需要指定域名,如果這里用本地化就需要指定域名。
默認(rèn)配置是不開(kāi)啟cookie的,還有就是用cookie不太好,每次啟動(dòng)exe都需要登錄太麻煩了,推薦使用localStorage永久保存。
我的cookie示例,打包exe時(shí)使用localStorage,其他情況下使用cookie:
setCookie(cname, cvalue) { if(process.env.NODE_ENV === "host"){ localStorage.setItem(cname, cvalue); }else{ document.cookie = cname + "=" + cvalue } // var d = new Date(); // d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); // var expires = "expires=" + d.toUTCString(); //document.cookie = cname + "=" + cvalue }, getCookie(cname) { if(process.env.NODE_ENV === "host"){ return localStorage.getItem(cname); }else{ var name = cname + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1); if (c.indexOf(name) != -1) { return c.substring(name.length, c.length); } } return ""; } }, clearCookie(cname) { if(process.env.NODE_ENV === "host"){ localStorage.removeItem(cname); }else{ var d = new Date(); d.setTime(-1); var expires = "expires=" + d.toUTCString(); document.cookie = cname + "=''; " + expires; } },
其他代碼一并粘出來(lái)分享一下。
main.js
const env = process.env.NODE_ENV || 'development'; if(env === "host"){//如果是打包exe需要指定接口域名 axios.defaults.baseURL = process.env.VUE_APP_INTERFACE axios.defaults.withCredentials = true;//跨域請(qǐng)求的全局憑證 }
package.json
"scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "lint": "vue-cli-service lint", "electron:build": "vue-cli-service build --mode host && electron-builder",//使用.env.host配置打包exe "electron:serve": "electron ." }, "build": { "productName": "newsaas", "appId": "com.example.newsaas", "win": { "icon": "./icon256.png",//這里是啟動(dòng)圖標(biāo)必須是256*256的png圖 "target": [ "nsis", "appx" ] }, "mac": { "target": "dmg" }, "nsis": { "oneClick": true, "allowToChangeInstallationDirectory": true, "perMachine": true, "allowElevation": true, "createDesktopShortcut": true, "createStartMenuShortcut": true, "shortcutName": "index" }, "directories": { "output": "build" }, "files": [ "dist/**/*", "index.js", "public/favicon.ico" ] },
.dev
NODE_ENV="" VUE_APP_INTERFACE="這里是我的接口域名,只能本地開(kāi)發(fā)時(shí),做代理用,打包不影響"
.dev.host
NODE_ENV="host" VUE_APP_INTERFACE="http://這里是我的接口域名"
vue.config.js
const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ devServer:{ port:8080, allowedHosts: "all", webSocketServer:false, proxy:{ "/":{ target: process.env.VUE_APP_INTERFACE, changeOrigin:true, pathRewrite:{ "^/":"/" } } } }, transpileDependencies: true, publicPath: './', // 輸出文件目錄 assetsDir: 'static', outputDir: process.env.outputDir, // eslint-loader 是否在保存的時(shí)候檢查 lintOnSave: true, // 生產(chǎn)環(huán)境是否生成 sourceMap 文件 productionSourceMap: false })
以上就是全部代碼,如果安裝不了electron,或是安裝后運(yùn)行不起來(lái),改一下npm鏡像源試試,package-lock.json這個(gè)文件記得刪除。
到此這篇關(guān)于vue3使用Electron打包成exe的方法與打包報(bào)錯(cuò)解決的文章就介紹到這了,更多相關(guān)Electron打包成exe與報(bào)錯(cuò)解決內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue配置electron使用electron-builder進(jìn)行打包的操作方法
- 使用electron打包Vue前端項(xiàng)目的詳細(xì)流程
- 如何使用electron將vue項(xiàng)目打包成.exe文件(保姆級(jí)教程)
- 解決electron打包vue-element-admin項(xiàng)目頁(yè)面無(wú)法跳轉(zhuǎn)的問(wèn)題小結(jié)
- vue項(xiàng)目使用electron進(jìn)行打包操作的全過(guò)程
- 手把手教你使用electron將vue項(xiàng)目打包成exe
- 關(guān)于electron-vue打包后運(yùn)行白屏的解決方案
- vue項(xiàng)目打包成桌面快捷方式(electron)的方法
- 用electron打包vue項(xiàng)目中的報(bào)錯(cuò)問(wèn)題及解決
- vue 項(xiàng)目集成 electron 和 electron 打包及環(huán)境配置方法
相關(guān)文章
vue3+vite+ts使用monaco-editor編輯器的簡(jiǎn)單步驟
因?yàn)楫呍O(shè)需要用到代碼編輯器,根據(jù)調(diào)研,我選擇使用monaco-editor代碼編輯器,下面這篇文章主要給大家介紹了關(guān)于vue3+vite+ts使用monaco-editor編輯器的簡(jiǎn)單步驟,需要的朋友可以參考下2023-01-01詳解vue中使用express+fetch獲取本地json文件
本篇文章主要介紹了詳解vue中使用express+fetch獲取本地json文件,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10父子組件生命周期及子組件獲取數(shù)據(jù)傳值問(wèn)題剖析
這篇文章主要介紹了父子組件生命周期及子組件獲取數(shù)據(jù)問(wèn)題剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10vue對(duì)象添加屬性(key:value)、顯示和刪除屬性方式
這篇文章主要介紹了vue對(duì)象添加屬性(key:value)、顯示和刪除屬性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07VNode虛擬節(jié)點(diǎn)實(shí)例簡(jiǎn)析
這篇文章主要介紹了VNode虛擬節(jié)點(diǎn),結(jié)合實(shí)例形式分析了VNode虛擬節(jié)點(diǎn)的基本功能、原理與實(shí)現(xiàn)方法,需要的朋友可以參考下2023-06-06elementui之el-table如何通過(guò)v-if控制按鈕顯示與隱藏
這篇文章主要介紹了elementui之el-table如何通過(guò)v-if控制按鈕顯示與隱藏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11