在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文件中可能无法生效,可使用另一种方法,详细请看 Vue项目 js文件获取json 配置文件的配置信息