AntDesignPro使用electron構(gòu)建桌面應(yīng)用示例詳解
注意事項(xiàng)聲明
- 所有
node包必須使用npm安裝不可使用cnpm, 使用cnpm安裝的node包會(huì)導(dǎo)致打包時(shí)間無(wú)限可能 - 具體區(qū)別查看使用
npm和cnpm安裝的包結(jié)構(gòu) - 所有包的均可以安裝在全局, 避免重復(fù)安裝
主要分為兩個(gè)部分
開(kāi)發(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();//打開(kāi)調(diào)試工具
?
//測(cè)試時(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)入 未做此操作無(wú)法在其他地方使用 electron 模塊
global.electron = require('electron')
在 package.json 文件中加入啟動(dòng)命令
"scripts": {
"electron-start": "electron .",
},
試啟動(dòng) 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: './', //使打包后的文件使用相對(duì)路徑
src/utils/request.js 此目錄并非標(biāo)準(zhǔn) 不同版本下文件可能有所區(qū)別 重點(diǎn)在于給請(qǐng)求配置前綴
當(dāng)項(xiàng)目打包成應(yīng)用后使用的是 file:協(xié)議 ant pro 的請(qǐng)求無(wú)法發(fā)出 需要使用完整的請(qǐng)求地址 目前方法為配置前綴
/**
* 配置request請(qǐng)求時(shí)的默認(rèn)參數(shù)
*/
const request = extend({
errorHandler, // 默認(rèn)錯(cuò)誤處理
prefix: 'http://hotel-system.yc384.com/api', // 請(qǐng)求前綴
credentials: 'include', // 默認(rèn)請(qǐng)求是否帶上cookie
});
package.json配置打包后的路徑方式
"homepage": ".",
使用 electron-builder 打包 exe 文件或者安裝包,壓縮包
- 提示:
- 提前安裝在全局可以省略不同環(huán)境重復(fù)安裝
- 創(chuàng)建
app目錄是為了不將node包打包進(jìn)去,減少應(yīng)用大小 - 如果當(dāng)前目錄下沒(méi)有
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)容,否則會(huì)打包當(dāng)前所有內(nèi)容)
將ant pro打包后的dist文件和main.js放入app目錄
在app下創(chuàng)建package.json文件(外層package做打包使用,app下的package是打包后的應(yīng)用依賴(lài))
"name": "hotel", "version": "2.3.1", "main": "main.js",
執(zhí)行打包命令
打包后文件會(huì)在 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)境可以和開(kāi)發(fā)環(huán)境分開(kāi) 這樣可以減少不必要依賴(lài) 縮短打包時(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)建桌面的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序 action-sheet詳解及實(shí)例代碼
這篇文章主要介紹了微信小程序 action-sheet詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11
arcgis?js完整懸停效果實(shí)現(xiàn)demo
這篇文章主要為大家介紹了arcgis?js完整懸停效果實(shí)現(xiàn)demo詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Hardhat進(jìn)行合約測(cè)試環(huán)境準(zhǔn)備及方法詳解
這篇文章主要為大家介紹了Hardhat進(jìn)行合約測(cè)試環(huán)境準(zhǔn)備及方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
微信小程序-getUserInfo回調(diào)的實(shí)例詳解
這篇文章主要介紹了微信小程序-getUserInfo回調(diào)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
dotenv源碼解讀從.env文件中讀取環(huán)境變量
這篇文章主要為大家介紹了dotenv源碼解讀從.env文件中讀取環(huán)境變量示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
微信小程序 動(dòng)畫(huà)的簡(jiǎn)單實(shí)例
這篇文章主要介紹了微信小程序 動(dòng)畫(huà)的簡(jiǎn)單實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10

