当前位置:网站首页>How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
2022-06-24 08:56:00 【Haoyanfeng VIP】
Preface
The premise of this blog post is uniapp Is based on vue3.0 + vite + HbuilderX 3.4.15.20220610 In the context of version
- 1, Use uniapp When developing cross end projects , I'm using HbuilderX Developed , because HbuilderX To publish a project, click publish in the menu bar , Publish only one environment , You have to change the code to switch environment variables every time , Very trouble , It may also be missed , So I studied how to distinguish the environment, package and go online .
- 2, Then someone will ask , Why not vue-cli Develop packaged projects , Direct use .env Documents or `cross-env Differentiate between environment variables , Note here that my project is to generate app Of , So it is inconvenient to use vue-cli, To develop .
Solution
I saw it in the official documents package.json Extended configuration usage , This article , Can solve this problem very well
- 1, This method also has defects , That is, you can't be here app Upper use , Only for web pages and applets .
- 2, I saw someone on the Internet propose to use appid Differentiate the environment , But I tried it myself , Discover... For each environment appid It's all the same , There is no way to distinguish the environment , This method was abandoned by me .
- 3, Then I thought of myself , Can it be like H5 equally , I am here app Make a function to switch the environment , Although only one bag can be packed , But the environment can be switched within the package , Can this solve the problem
- 4, After practice , My idea was confirmed , May adopt .
First step : install package.json file
If you have this file in your project , Just ignore this step , without , Just look down .
Use npm init -y establish package.json file , This file can be found in the root directory of the project after it is created
The second step : To configure package.json file
Be careful : Official documents say , This file cannot have comments , So to copy the code, delete the comments . There is a document gate on it
{
"name": "uniuiTemplate",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
// The following is the configuration of all environment variables of different platforms that you can configure
"uni-app": {
"scripts": {
"h5dev":{
"title":"h5 Development Edition ",
"browser":"chrome",
"env": {
"UNI_PLATFORM": "h5", // Benchmark platform , You must write the value allowed by the platform
"VUE_APP_BASE_URL":"http://127.0.0.1:h5dev/" // Custom environment variables
},
"define":{
"H5-DEV":true
}
},
"h5test":{
"title":"h5 The beta ",
"browser":"chrome",
"env": {
"UNI_PLATFORM": "h5",
"VUE_APP_BASE_URL":"http://127.0.0.1:h5test/",
"MY_TEST":"test-variable"
},
"define":{
"H5-TEST":true
}
},
"h5prod":{
"title":"h5 Production version ",
"browser":"chrome",
"env": {
"UNI_PLATFORM": "h5",
"VUE_APP_BASE_URL":"http://127.0.0.1:h5prod/"
},
"define":{
"H5-PROD":true
}
},
"mp-weixin-dev":{
"title":" Wechat development version ",
"env": {
"UNI_PLATFORM": "mp-weixin",
"VUE_APP_BASE_URL":"http://127.0.0.1:mp-weixin-dev/"
},
"define":{
"MP-WEIXIN-DEV":true
}
},
"mp-weixin-test":{
"title":" Wechat beta ",
"env": {
"UNI_PLATFORM": "mp-weixin",
"VUE_APP_BASE_URL":"http://127.0.0.1:mp-weixin-test/"
},
"define":{
"MP-WEIXIN-TEST":true
}
},
"mp-weixin-prod":{
"title":" Wechat production version ",
"env": {
"UNI_PLATFORM": "mp-weixin",
"VUE_APP_BASE_URL":"http://127.0.0.1:mp-weixin-prod/"
},
"define":{
"MP-WEIXIN-PROD":true
}
}
}
}
}
The third step : To configure package.json Changes and operations after the completion of the document
After the configuration is completed, you will find HbuilderX The editor has a few more environment variables , As follows
function
Click on
functionYou will find several more options , Here's the picture : Then you can choose the environment you want to run to develop .
issue
Click on
issueYou will find several more options , Here's the picture : Then you can choose the environment you want to release for deployment or upload code .
Conclusion
Here we are , You can use hbuilderx Develop or publish different environments and platforms , Say goodbye to manually switching environment variables .
Tips: This configuration uses vue-cli It's OK, too .
边栏推荐
- Summary of methods in numpy
- [quantitative investment] discrete Fourier transform to calculate array period
- Using skills of xargs -- the way to build a dream
- Opencv maximum filtering (not limited to images)
- Matlab camera calibrator camera calibration
- Change SSH port number
- “不平凡的代理初始值设定不受支持”,出现的原因及解决方法
- 【LeetCode】541. 反转字符串 II
- GradScaler MaxClipGradScaler
- 陆奇:我现在最看好这四大技术趋势
猜你喜欢

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

110. balanced binary tree recursive method

Data middle office: middle office architecture and overview

MBA-day25 最值问题-应用题

The form image uploaded in chorme cannot view the binary image information of the request body

【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理

KaFormer个人笔记整理

MySQL | 视图《康师傅MySQL从入门到高级》笔记

一文讲透,商业智能BI未来发展趋势如何

“论解不了数独所以选择做个数独游戏这件事”
随机推荐
项目部署相关
[pytoch basic tutorial 31] youtubednn model analysis
Lombok use
Scheme of alcohol concentration tester based on single chip microcomputer
【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
4274. 后缀表达式
不能改变虚拟机电源状态报错解决方案
1704. 判断字符串的两半是否相似
【LeetCode】415. 字符串相加
偶然间得到的framework工具类 自用
520. 检测大写字母
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
数据中台:数据采集和抽取的技术栈详解
[quantitative investment] discrete Fourier transform to calculate array period
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
解决:模型训练时loss出现nan
数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题
Double pointer analog
The pie chart with dimension lines can set various parameter options
用VNC Viewer的方式远程连接无需显示屏的树莓派

