当前位置:网站首页>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 .
边栏推荐
猜你喜欢

【牛客】把字符串转换成整数

Mysql数据(Liunx环境)定时备份

Qingcloud based "real estate integration" cloud solution

华为路由器:GRE技术

2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。

every()、map()、forEarch()方法。数组里面有对象的情况

ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

4275. Dijkstra序列

Xiaohei ai4code code baseline nibble 1
随机推荐
Get screen width and height tool class
MySQL | view notes on Master Kong MySQL from introduction to advanced
4275. Dijkstra序列
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
Telnet port login method with user name for liunx server
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
数据中台:数据中台全栈技术架构解析,附带行业解决方案
leetcode——错误的集合
关于 GIN 的路由树
第七章 操作位和位串(三)
opencv最大值滤波(不局限于图像)
tcpdump抓包实现过程
双指针模拟
Database migration from PostgreSQL to MySQL
WebRTC系列-网络传输之5选择最优connection切换
打印出来的对象是[object object],解决方法
110. balanced binary tree recursive method
關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL
520. detect capital letters
数据中台:国内大厂中台建设架构集锦

