日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

怎么把html封装成桌面应用,如何将一个现有的Vue网页项目封装成electron桌面应用...

發布時間:2024/3/12 vue 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 怎么把html封装成桌面应用,如何将一个现有的Vue网页项目封装成electron桌面应用... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求緣由

開發了很久的網頁項目,領導突然想變成桌面應用,又要重寫代碼?no,no,no。不可能的。重寫代碼吃力不討好,浪費時間,浪費精力。那么我們該怎么辦呢?

幸好,electron是如此神奇,它提供了一種能讓你將任何前端代碼的網站頁面封裝成桌面應用。無論是vue,react 還是 angular等現有的框架都能實現。

基礎

學習該內容需要基本的 javascript 語法基礎,以及 node.js 基礎。

步驟

1,安裝electron依賴包(局部安裝,只安裝開發依賴)

npm install electron --save-dev

2,package.json 同級位置創建文件 electron.js

electron.js 文件內容:

// 主進程

// Modules to control application life and create native browser window

const {app, protocol, Menu, BrowserWindow } = require('electron')

const path = require('path')

const { readFile } = require ('fs')

const { URL } = require ('url')

// 處理文件打包之后的訪問路徑為 app的相對路徑,

function createProtocol(scheme) {

protocol.registerBufferProtocol(

scheme,

(request, respond) => {

let pathName = new URL(request.url).pathname

pathName = decodeURI(pathName) // Needed in case URL contains spaces

readFile(path.join(__dirname, pathName), (error, data) => {

if (error) {

console.error(`Failed to read${pathName}on${scheme}protocol`, error)

}

const extension = path.extname(pathName).toLowerCase()

let mimeType = ''

if (extension === '.js') {

mimeType = 'text/javascript'

} else if (extension === '.html') {

mimeType = 'text/html'

} else if (extension === '.css') {

mimeType = 'text/css'

} else if (extension === '.svg' || extension === '.svgz') {

mimeType = 'image/svg+xml'

} else if (extension === '.json') {

mimeType = 'application/json'

}

respond({ mimeType, data })

})

},

error => {

if (error) {

console.error(`Failed to register${scheme}protocol`, error)

}

}

)

}

// 防止 localStorage 不可訪問

protocol.registerSchemesAsPrivileged([{

scheme: 'app',

privileges: {

secure: true,

standard: true

}

}])

// Keep a global reference of the window object, if you don't, the window will

// be closed automatically when the JavaScript object is garbage collected.

let mainWindow

let template = [] //頂部菜單模板

function createWindow () {

// Create the browser window. 桌面應用的初始寬度、高度

mainWindow = new BrowserWindow({

width: 1600,

height: 1000,

})

// and load the index.html of the app.

createProtocol('app') // 創建一個應用,訪問路徑的初始化

mainWindow.loadURL('app://./index.html') // 入口文件, 窗口指向 index.html 文件作為首頁,這里可以是vue,react,angular 的打包之后的dist目錄內部的index.html文件。

// Open the DevTools.

// mainWindow.webContents.openDevTools()

let menu = Menu.buildFromTemplate(template)

Menu.setApplicationMenu(menu) // 重新設置桌面應用的頂部菜單

// Emitted when the window is closed.

mainWindow.on('closed', function () {

// Dereference the window object, usually you would store windows

// in an array if your app supports multi windows, this is the time

// when you should delete the corresponding element.

mainWindow = null

})

}

// 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.on('ready', createWindow)

// Quit when all windows are closed.

app.on('window-all-closed', function () {

// On macOS it is common for applications and their menu bar

// to stay active until the user quits explicitly with Cmd + Q

if (process.platform !== 'darwin') app.quit()

})

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 (mainWindow === null) createWindow()

})

// 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文件中增加指令,用來啟動Electron桌面應用

“scripts”: {

“dev”: “vue-cli-service serve”,

“build”: “vue-cli-service build”,

“electron-dev”: “vue-cli-service build && electron electron.js”, ------新增

“electron-build”: “electron-packager ./dist/ --arch=x64 --icon=logo.ico --overwrite” ------ 新增,打包成.exe 文件使用

},

備注:注意添加文件 logo.ico 到 package.json 的同級位置。

這里需要靈活變化:

1,vue-cli-service build 是打包成dist文件的命令,具體項目的打包命令具體替換,本項目使用了vue-cli腳手架,故命令如此。

2,./dist/ 是指生成的桌面應用是在dist文件內部的基礎上打包的,因此要看你的index.html文件在哪里,一般build之后都有一個index.html文件。

4,在關于webpack的基礎配置文件里的module.exports 內部添加一條數據 publicPath: ‘./’,

這里是為了讓build之后的文件引用相對路徑。

5,將所有的api 請求的前面都加上對應的網站域名。

這里最好有一個公用的函數,一般都是在axios 的 baseURL參數里面。

6,先執行 npm run electron_dev,可以運行開發中的electron桌面應用。

接下來就是怎么打包出桌面應用了,這里用的是打包工具electron-packager ,當然也可以使用別的工具。

7,安裝 electron-packager

npm install electron-packager --save-dev

8,復制 package.json 到dist文件里,新增一條數據 “main”: “electron.js”,

9,復制 electron.js 到dist文件里,

10,執行 npm run electron_build ,便會生成安裝包文件 vue-antd-pro-win32-x64 , 打開里面的 .exe 文件即可

總結

總體來說,代碼不多,步驟也不多,但具體項目又可能遇到各種不可預測的問題,仍然需要具體問題具體分析。

參考內容

https://www.cnblogs.com/liulun/p/12984456.html

本文地址:https://blog.csdn.net/Aglaia_web/article/details/107543945

總結

以上是生活随笔為你收集整理的怎么把html封装成桌面应用,如何将一个现有的Vue网页项目封装成electron桌面应用...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。