-
先在项目静态资源文件夹static下创建一个config.json文件
图片.png -
在config.json文件中设置要配置的url地址
图片.png -
然后可以通过两种方式来实现
a. 在项目非静态资源文件夹中创建一个js文件
图片.png
通过xhr来请求出json中的数据
// 定义配置变量
let config
// 定义同步获取json文件数据方法
let syncGetJsonData = function (url) {
let xhr = new XMLHttpRequest()
xhr.open('get', url, false)
xhr.send()
return JSON.parse(xhr.responseText)
}
config = syncGetJsonData('./static/json/config.json?r=' + Math.random())
export default config
然后再main.js中引入,挂载到Vue.prototype上
图片.png
b. 另一种方式就是直接main.js中去请求config.json的数据,然后挂载到Vue.prototype上,同时把Vue实例也放在请求返回结果中(request.request是我自己封装的请求方法,替换成自己的或者axios就行)
request.request({
url: './static/json/config.json',
method: 'GET'
}).then(res => {
Vue.prototype.$urllist = res.data;
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
})