# uniapp项目打包为桌面应用 **Repository Path**: lbm526/tobeExe ## Basic Information - **Project Name**: uniapp项目打包为桌面应用 - **Description**: uniapp项目打包为桌面应用步骤解读 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2022-09-30 - **Last Updated**: 2025-01-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: Web ## README # uniapp项目打包为桌面应用 #### 介绍 uniapp项目打包为桌面应用步骤解读 #### 项目准备 1. 修改uniapp配置--manifest.json ![输入图片说明](h5/static/files1.png) 运行的基础路径修改为:./ 不然打包出来会出现白屏,读取不到,因为打包出来的h5默认加载地址为/static/ 去掉启用https协议: 不然会出现网络无法加载,去掉https不影响你请求后端的https协议。 2. 打包项目为H5 ![输入图片说明](h5/static/files2.png) #### 安装教程 1. cnpm install electron -g 2. cnpm install electron-packager -g #### 使用说明 1. H5文件夹下新建package.json和main.js ![输入图片说明](h5/static/files3.png) - package.json ``` { "productName": "test", "name": "PDA应用", "version": "1.0.2", "description": "A minimal Electron application", "main": "main.js", "scripts": { "start": "electron .", // PDA应用(应用名称)、--icon应用图标、--out输出文件名、--app-version版本 "package": "electron-packager . PDA应用 --platform=win32 --arch=x64 --icon=./a.ico --out=PDA应用 --asar --app-version=1.0.2 --overwrite --ignore=node_modules" }, "keywords": [ "Electron", "quick", "start", "tutorial", "demo" ], "author": "LBM", //作者名 "devDependencies": { "electron": "^18.0.1" } } ``` 2. main.js ``` const {app, BrowserWindow} = 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 win function createWindow () { // Create the browser window. // 窗体宽高、程序左上角图标 win = new BrowserWindow({width: 1000, height: 1000,icon: path.join(__dirname,'./portrait.png')}) // and load the index.html of the app. win.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'),//启动初始页面 protocol: 'file:', slashes: true })) // Open the DevTools. // win.webContents.openDevTools() // Emitted when the window is closed. win.on('closed', () => { // 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. win = 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', () => { // 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', () => { // 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 (win === 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. npm run package打包,等待完成 ####打包完成 完成后生成下列文件,打开.exe文件就OK ![输入图片说明](h5/static/files4.png)