vue2开发环境与正式环境配置

1、首先添加两个文件,一个是正式环境的,一个开发环境的(我所在的公司项目只有两个环境,测试环境和正式环境,所以开发环境其实也就是测试环境的api地址),文件名最好直接复制,避免敲错排查问题会浪费开发时间

.env.development // 开发环境(也可以是测试环境,具体看个人配置)
.env.production  //  正式环境

在这里插入图片描述

2、.env.development 文件的内容,属性必须以VUE_APP_开头。

NODE_ENV = 'development'
VUE_APP_LOGIN_URL = 'https://tst-xxxxxxx.com' // 开发(测试)环境API地址

.env.production 文件的内容,属性必须以VUE_APP_开头。

NODE_ENV = 'production'
VUE_APP_LOGIN_URL = 'https://tst-xxxxxxx.com' // 正式环境API地址

3、request中使用,process.env 是全局的

const service = axios.create({
    
    
  baseURL: process.env.VUE_APP_LOGIN_URL,  // 在请求中进行配置
  timeout: 10000,
  method
});

4、package.json中配置

"scripts": {
    
    
    "serve": "vue-cli-service serve --mode development",  // npm run serve 本地运行测试(开发)环境
    "formal": "vue-cli-service serve --mode production",  // npm run formal 本地运行正式环境
    "build": "vue-cli-service build --mode production",  // npm run build 打包正式环境
    "test": "vue-cli-service build --mode development"  // npm run test 打包测试(开发)环境
 },

猜你喜欢

转载自blog.csdn.net/weixin_44949068/article/details/129406162
今日推荐