config文件夹下
dev.env.js中修改代码
‘use strict‘ const merge = require(‘webpack-merge‘) const prodEnv = require(‘./prod.env‘) module.exports = merge(prodEnv, { NODE_ENV: ‘"development"‘, HOST: ‘"dev"‘ })
prod.env.js中修改代码 HOST为截取到的参数
‘use strict‘ let HOST = process.argv.splice(2)[0] || ‘prod‘; module.exports = { NODE_ENV: ‘"production"‘, HOST: ‘"‘+HOST+‘"‘ }
在封装axios请求的文件中
/** * 环境切换 */ let apiUrl = ‘‘ let HOST = process.env.HOST; if(HOST === ‘prod‘){ apiUrl = ‘http://10.1.28.221:12001‘ } if(HOST === ‘test‘){ apiUrl = ‘http://10.88.10.31:12002‘ } axios.defaults.baseURL = apiUrl;
这样只需在打包的时候带上参数就可以自动切换不同的url
生产环境:npm run build -- prod
测试环境:npm run build --test
原文地址:https://www.cnblogs.com/aizz/p/10090996.html
时间: 2024-11-05 13:40:40