vue2设置运行环境和打包环境变量

环境

javascript 复制代码
 env ------ 所有模式共用
.env.local ------ 所有模式共用,但不会被 git 提交,自动添加到.gitignore
.env.development ------ 仅开发模式使用,使用npm run server时默认会加载此配置文件
.env.developmen.local------ 仅开发模式使用,但会被 git 忽略自动添加到.gitignore,使用npm run server时默认会加载此配置文件
.env.production ------ 仅生产模式使用,使用npm run build时默认会加载此配置文件
.env.staging------ 仅staging模式使用,使用需要配置--mode staging

.env.dev
.env.test
.env.prod

编写配置内容

javascript 复制代码
# 运行环境
NODE_ENV="development"
  
# 设置Axios 基础路径
VUE_APP_BASE_URL = "/api/"

# 设置App运行路径
VUE_APP_MAPP_URL ="https://t.m.xxxx.com" 

# 是否使用Hash路由
VUE_APP_MAPP_URL = 'true'
想要运行环境来设置的话 就可以在这里设置 但是有1点需要注意  这里的前缀必须是VUE_APP_开头
其他的识别不到

在package.json文件中编写启动命令

这里的 env 文件是会默认加载 其他的都需要手动在这里添加启动命令

javascript 复制代码
"scripts": {
    "dev": "node --max_old_space_size=4096 node_modules/@vue/cli-service/bin/vue-cli-service.js serve --mode dev",
    "test": "node --max_old_space_size=4096 node_modules/@vue/cli-service/bin/vue-cli-service.js serve --mode test",
    "prod": "node --max_old_space_size=4096 node_modules/@vue/cli-service/bin/vue-cli-service.js serve --mode prod",
    "build:dev": "vue-cli-service build --mode dev",
    "build:test": "vue-cli-service build --mode test",
    "build:prod": "vue-cli-service build --mode prod",
  }

使用环境变量

复制代码
启动后 可以随意页面调用配置运行环境里面的  process.env.xxx   

示例 vue2

vue2 使用的是webpack 和vue3使用vite写法不一样哦!!!

javascript 复制代码
const { defineConfig } = require("@vue/cli-service");
const { getProxyConfig } = require('./src/config/proxy');

module.exports = defineConfig({
  publicPath: "/",
  transpileDependencies: true,
  outputDir: "./dist",//打包路径
  devServer: {
    port: 10001,
    proxy: getProxyConfig(process.env.NODE_ENV)//这里传入env运行环境去动态设置proxy
  }
});
javascript 复制代码
function getProxyConfig (env) {
  const urlConfig = {
    '/api': {
      'development': 'http://xxx:10000/api/',
      'test': 'https://xxx/api',
      'production': 'https://xxx/api',
    },
    '/pay': {
      'development': 'http://xxx:10000/pay',
      'test': 'http:/xxx/pay',
      'production': 'https://xxx.com',
    }
  }

  const target = (targetTag, baseUrl) => ({
    target: targetTag,
    changeOrigin: true,
    pathRewrite: {
      [`^${baseUrl}`]: '/',
    },
  });

  return Object.entries(urlConfig).reduce((result, [key, value]) => {
    result[key] = target(value[env], key);
    return result;
  }, {}); 
}

module.exports = { getProxyConfig };
相关推荐
apcipot_rain1 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
油丶酸萝卜别吃1 小时前
OpenLayers 精确经过三个点的曲线绘制
javascript
ShallowLin1 小时前
vue3学习——组合式 API:生命周期钩子
前端·javascript·vue.js
Nejosi_念旧2 小时前
Vue API 、element-plus自动导入插件
前端·javascript·vue.js
互联网搬砖老肖2 小时前
Web 架构之攻击应急方案
前端·架构
pixle02 小时前
Vue3 Echarts 3D饼图(3D环形图)实现讲解附带源码
前端·3d·echarts
麻芝汤圆3 小时前
MapReduce 入门实战:WordCount 程序
大数据·前端·javascript·ajax·spark·mapreduce
juruiyuan1114 小时前
FFmpeg3.4 libavcodec协议框架增加新的decode协议
前端
Peter 谭5 小时前
React Hooks 实现原理深度解析:从基础到源码级理解
前端·javascript·react.js·前端框架·ecmascript
周胡杰5 小时前
鸿蒙接入flutter环境变量配置windows-命令行或者手动配置-到项目的创建-运行demo项目
javascript·windows·flutter·华为·harmonyos·鸿蒙·鸿蒙系统