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