Vue使用electron轉(zhuǎn)換項(xiàng)目成桌面應(yīng)用方法介紹
1、將已有 Vue 項(xiàng)目打包。
2、新建 main.js、package.json 文件
將打包生成的 index.html、js、css、然后再和新建的 main.js、package.json 文件 放至一個(gè)目錄下。并命令行切換至這個(gè)目錄。
新建的 main.js 如下:
const { app, BrowserWindow } = require("electron"); // 引入electron let win; let windowConfig = { width: 800, height: 600, }; // 窗口的大小 function createWindow() { win = new BrowserWindow(windowConfig); // 創(chuàng)建一個(gè)窗口 win.loadURL(`file://${__dirname}/index.html`); // 加載打包生成的index.html win.webContents.openDevTools(); // 開啟調(diào)試工具 win.on("close", () => { //回收 BrowserWindow 對(duì)象 win = null; }); win.on("resize", () => { win.reload(); }); } app.on("ready", createWindow); app.on("window-all-closed", () => { app.quit(); }); app.on("activate", () => { if (win == null) { createWindow(); } });
新建的 package.json 如下:
{ "name": "demo", "productName": "項(xiàng)目名稱", "author": "作者", "version": "1.0.4", "main": "main.js", "description": "項(xiàng)目描述", "scripts": { "pack": "electron-builder --dir", "dist": "electron-builder", "postinstall": "electron-builder install-app-deps" }, "build": { "electronVersion": "1.8.4", "win": { "requestedExecutionLevel": "highestAvailable", "target": [ { "target": "nsis", "arch": [ "x64" ] } ] }, "appId": "demo", "artifactName": "demo-${version}-${arch}.${ext}", "nsis": { "artifactName": "demo-${version}-${arch}.${ext}" }, "extraResources": [ { "from": "./static/xxxx/", "to": "app-server", "filter": [ "**/*" ] } ] }, "dependencies": { "core-js": "^2.4.1", "electron-packager": "^12.1.0", "electron-updater": "^2.22.1" }, "devDependencies": { "electron-forge": "^5.2.4" } }
3、安裝包:
yarn install
(報(bào)錯(cuò)不用管,能 electron .
運(yùn)行成功且效果正常就行)
4、運(yùn)行:
electron .
5、注意事項(xiàng):
- vue 項(xiàng)目 路由用
hash
模式。 - vue 項(xiàng)目 的 vue.config.js 要配置
publicPath: './'
(因?yàn)槿舨慌渲?,則 electron 文件路徑不對(duì))
module.exports = { lintOnSave: false, publicPath: './', css: .... .... }
index.html 中文件路徑如以下正確顯示:
<link rel="icon" href="favicon.ico" rel="external nofollow" > <title>efficiency-helper</title> <link href="css/chunk-11991773.33db9af5.css" rel="external nofollow" rel="prefetch"> <link href="css/chunk-17ca335a.ad6ca46b.css" rel="external nofollow" rel="prefetch"> <link href="css/chunk-3dde8fae.019eaf8d.css" rel="external nofollow" rel="prefetch"> <link href="css/chunk-4c9aec9b.410cb728.css" rel="external nofollow" rel="prefetch"> <link href="css/chunk-f52405ee.f4abe7d9.css" rel="external nofollow" rel="prefetch">
若不配置 publicPath: './'
則:href=“/css/chunk-17ca335a.ad6ca46b.css” 路徑錯(cuò)誤。導(dǎo)致應(yīng)用出現(xiàn)白屏。
到此這篇關(guān)于Vue使用electron轉(zhuǎn)換項(xiàng)目成桌面應(yīng)用方法介紹的文章就介紹到這了,更多相關(guān)Vue electron內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 打包的靜態(tài)文件不能直接運(yùn)行的原因及解決辦法
這篇文章主要介紹了Vue 打包的靜態(tài)文件不能直接運(yùn)行的原因及解決辦法,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下2020-11-11element 結(jié)合vue 在表單驗(yàn)證時(shí)有值卻提示錯(cuò)誤的解決辦法
這篇文章主要介紹了element 結(jié)合vue 在表單驗(yàn)證下,有值卻提示錯(cuò)誤的解決辦法,需要的朋友可以參考下2018-01-01vant-ui組件庫(kù)中如何修改NavBar導(dǎo)航欄的樣式
這篇文章主要介紹了vant-ui組件庫(kù)中如何修改NavBar導(dǎo)航欄的樣式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue實(shí)現(xiàn)在線預(yù)覽pdf文件功能(利用pdf.js/iframe/embed)
項(xiàng)目要求需要預(yù)覽pdf文件,網(wǎng)上找了很久,發(fā)現(xiàn)pdf.js的效果,這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)在線預(yù)覽pdf文件功能,主要利用pdf.js/iframe/embed來實(shí)現(xiàn)的,需要的朋友可以參考下2021-06-06Vue2利用Axios發(fā)起請(qǐng)求的詳細(xì)過程記錄
有很多時(shí)候你在構(gòu)建應(yīng)用時(shí)需要訪問一個(gè)API并展示其數(shù)據(jù),做這件事的方法有好幾種,而使用基于promise的HTTP客戶端axios則是其中非常流行的一種,這篇文章主要給大家介紹了關(guān)于Vue2利用Axios發(fā)起請(qǐng)求的詳細(xì)過程,需要的朋友可以參考下2021-12-12vuex存儲(chǔ)數(shù)組(新建,增,刪,更新)并存入localstorage定時(shí)刪除功能實(shí)現(xiàn)
這篇文章主要介紹了vuex存儲(chǔ)數(shù)組(新建,增,刪,更新),并存入localstorage定時(shí)刪除,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Vue 設(shè)置axios請(qǐng)求格式為form-data的操作步驟
今天小編就為大家分享一篇Vue 設(shè)置axios請(qǐng)求格式為form-data的操作步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10electron-vue?運(yùn)行報(bào)錯(cuò)?Object.fromEntries?is?not?a?function
Object.fromEntries()?是?ECMAScript?2019?新增的一個(gè)靜態(tài)方法,用于將鍵值對(duì)列表(如數(shù)組)轉(zhuǎn)換為對(duì)象,如果在當(dāng)前環(huán)境中不支持該方法,可以使用?polyfill?來提供類似功能,接下來通過本文介紹electron-vue?運(yùn)行報(bào)錯(cuò)?Object.fromEntries?is?not?a?function的解決方案2023-05-05