VUE项目添加json配置文件


在vue项目中,有一些配置不想在代码里写死,可以写到json文件中

json文件:\static\config.json

{
  "detectionInterval": 15000,
  "intervalTimeout": 300000
}

src\main.js

Vue.prototype.$http = axios
Vue.prototype.getConfig = function() {
  this.$http.get('/static/config.json').then(res => {
    Vue.prototype.detectionInterval = res.data.detectionInterval
    Vue.prototype.intervalTimeout = res.data.intervalTimeout
  }).catch(err => {
    console.log(err)
  })
}

这样就在vue文件中就可以使用this.来获取数据了

getData() {
      console.log('intervalTimeout:{}', this.intervalTimeout)
}

但是此方法在js文件中可能无法生效,可使用另一种方法,详细请看 https://www.cnblogs.com/qliu85/p/15571383.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM