AntDesignPro使用electron構(gòu)建桌面應(yīng)用示例詳解
注意事項(xiàng)聲明
- 所有
node
包必須使用npm
安裝不可使用cnpm
, 使用cnpm
安裝的node
包會導(dǎo)致打包時(shí)間無限可能 - 具體區(qū)別查看使用
npm
和cnpm
安裝的包結(jié)構(gòu) - 所有包的均可以安裝在全局, 避免重復(fù)安裝
主要分為兩個(gè)部分
開發(fā)環(huán)境使用
安裝 electron 包
npm install electron --save-dev
package.js 添加入口文件
"main": "main.js",
創(chuàng)建 main.js 入口文件 內(nèi)容如下
const {app, BrowserWindow, dialog} = require('electron'); const path = require('path'); const url = require('url'); ? // 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; ? function createWindow () { ? // Create the browser window. mainWindow = new BrowserWindow({ width: 1300, height: 800, frame: true, autoHideMenuBar: true, fullscreenable: true, transparent: false, webPreferences: { javascript: true, plugins: true, nodeIntegration: true, // Nodejs webSecurity: false, preload: path.join(__dirname, './preload.js') } }); // mainWindow.webContents.openDevTools();//打開調(diào)試工具 ? //測試時(shí)使用mainWindow.loadURL(http://localhost:80000); //打包時(shí)加載本地文件 mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'dist/index.html'), protocol: 'file:', slashes: true })); ? // 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() });
preload.js 文件內(nèi)添加, 將 electron 做全局導(dǎo)入 未做此操作無法在其他地方使用 electron 模塊
global.electron = require('electron')
在 package.json 文件中加入啟動命令
"scripts": { "electron-start": "electron .", },
試啟動 electron 窗口內(nèi)容加載成功則成功
npm run electron-start
渲染進(jìn)程如需和主進(jìn)程通信查看官方文檔
https://electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes
打包應(yīng)用配置
config/config.js 文件添加
history: 'hash', //更改路由方式 publicPath: './', //使打包后的文件使用相對路徑
src/utils/request.js 此目錄并非標(biāo)準(zhǔn) 不同版本下文件可能有所區(qū)別 重點(diǎn)在于給請求配置前綴
當(dāng)項(xiàng)目打包成應(yīng)用后使用的是 file:協(xié)議
ant pro
的請求無法發(fā)出 需要使用完整的請求地址 目前方法為配置前綴
/** * 配置request請求時(shí)的默認(rèn)參數(shù) */ const request = extend({ errorHandler, // 默認(rèn)錯(cuò)誤處理 prefix: 'http://hotel-system.yc384.com/api', // 請求前綴 credentials: 'include', // 默認(rèn)請求是否帶上cookie });
package.json配置打包后的路徑方式
"homepage": ".",
使用 electron-builder 打包 exe 文件或者安裝包,壓縮包
- 提示:
- 提前安裝在全局可以省略不同環(huán)境重復(fù)安裝
- 創(chuàng)建
app
目錄是為了不將node
包打包進(jìn)去,減少應(yīng)用大小 - 如果當(dāng)前目錄下沒有
node
包或者內(nèi)容較少可直接在當(dāng)前操作, 省略app
目錄相關(guān)操作
安裝
npm install electron-builder
package.json添加命令 (打包windows)
"electron-build": "electron-builder --win --x64"
添加打包配置
"build": { "appId": "com.xxx.app", "directories": { "output": "build" }, // 打包后存放目錄 "mac": {// mac安裝包dmg "target": ["dmg","zip"] }, "win": {// win安裝包nsis "target": ["nsis","zip"] }
創(chuàng)建app目錄(builder默認(rèn)打包app下內(nèi)容,否則會打包當(dāng)前所有內(nèi)容)
將ant pro打包后的dist文件和main.js放入app目錄
在app下創(chuàng)建package.json文件(外層package做打包使用,app下的package是打包后的應(yīng)用依賴)
"name": "hotel", "version": "2.3.1", "main": "main.js",
執(zhí)行打包命令
打包后文件會在 build 目錄下
npm run electron-build
使用 electron-packager 打包成 exe 文件
安裝electron-package
npm install electron-packager --save-dev
package.json下script添加命令(具體含義百度)
"electron-package": "electron-packager . hotelSystem --win32 --out app --arch=x64 --overwrite --ignore=node_modulesls --electron-version=6.0.5",
執(zhí)行命令
npm run electron-package
提示
- 打包環(huán)境可以和開發(fā)環(huán)境分開 這樣可以減少不必要依賴 縮短打包時(shí)間
- 將打包后的
dist
和main.js
文件放入一個(gè)新目錄 - 配置
package.json
文件打包參數(shù) 其他刪除即可
"name": "hotel", "version": "2.3.1", "main": "main.js", "homepage": ".", "scripts": { "electron-start": "electron .", }
以上就是AntDesignPro使用electron構(gòu)建桌面應(yīng)用示例詳解的詳細(xì)內(nèi)容,更多關(guān)于AntDesignPro electron構(gòu)建桌面的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序 action-sheet詳解及實(shí)例代碼
這篇文章主要介紹了微信小程序 action-sheet詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11arcgis?js完整懸停效果實(shí)現(xiàn)demo
這篇文章主要為大家介紹了arcgis?js完整懸停效果實(shí)現(xiàn)demo詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Hardhat進(jìn)行合約測試環(huán)境準(zhǔn)備及方法詳解
這篇文章主要為大家介紹了Hardhat進(jìn)行合約測試環(huán)境準(zhǔn)備及方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03微信小程序-getUserInfo回調(diào)的實(shí)例詳解
這篇文章主要介紹了微信小程序-getUserInfo回調(diào)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10dotenv源碼解讀從.env文件中讀取環(huán)境變量
這篇文章主要為大家介紹了dotenv源碼解讀從.env文件中讀取環(huán)境變量示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12