一、根目录下找到或新建.env.test(测试环境)、env.development(开发环境)、.env.production文件(生产环境)
- .env.test(测试环境)配置内容
NODE_ENV = 'production'
VUE_APP_MODE = 'test'
VUE_APP_BASE_URL = http://******/ //这个地址不能带引号,不然获取的就带引号
VUE_APP_WS_URL= ws://********/ //这个地址不能带引号,不然获取的就带引号
outputDir = test//用来表示打包的名字
- .env.development(开发环境)配置内容
VUE_APP_MODE = 'development'
VUE_APP_BASE_URL = http:// ******
VUE_APP_WS_URL= ws:// *******
- .env.production文件(生产环境)配置内容
NODE_ENV = 'production'
VUE_APP_MODE = 'production'
VUE_APP_BASE_URL = http:// ******
VUE_APP_WS_URL= ws:// *******
二、在package.json中添加打包
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"test": "vue-cli-service serve--mode test",//新添加,build打包时,打包test环境,serve是运行时
"production": "vue-cli-service serve--mode production",//新添加,打包时,打包production环境
"development": "vue-cli-service build --mode development"//新添加,打包时,打包development环境
三、在文件中可以使用console.log(process.env.VUE_APP_BASE_URL);
this.$axios.post(process.env.VUE_APP_BASE_URL+"send/config/rf", formItems).then
原文参考:https://juejin.cn/post/6940815741423878175
若是使用的是webpack可参考:
https://blog.51cto.com/u_15785499/5664511