没有使用 package.json 进行配置 ,改配置时不需要重新打包 Vue 多环境配置 package.json
settings.js
window.environment="dev"
window.version = 'v1.0.1'
window.settings.dev = {
environment: '开发',
apiUrl: 'http://localhost:9091'
}
window.settings.test = {
environment: '测试',
apiUrl: 'http://192.168.0.100:9091'
}
window.settings.pre = {
environment: '预发布',
apiUrl: 'http://192.168.0.101:9091'
}
window.settings.pro = {
environment: '正式',
apiUrl: 'http://192.168.0.102:9091'
}
config/index.js
let _settings = '';
if (window.environment == 'dev') {
_settings = window.settings.dev
} else if (window.environment == 'test') {
_settings = window.settings.test
} else if (window.environment == 'pre') {
_settings = window.settings.pre
} else if (window.environment == 'pro') {
_settings = window.settings.pro
} else {
_settings = window.settings.dev
}
export default {
env: _settings,
}
vue
<div>{{config.env.environment}}</p>
<script>
import config from '@/config'
export default {
components: {
},
data(){
return {
config:config
}
}
}
</script>
<style>
</style>
main.js
/**
* @description 全局注册应用配置
*/
Vue.prototype.$config = config
---------
<div>{{$config.env.environment}}</p>