-
先在項目靜態(tài)資源文件夾static下創(chuàng)建一個config.json文件
圖片.png -
在config.json文件中設(shè)置要配置的url地址
圖片.png -
然后可以通過兩種方式來實現(xiàn)
a. 在項目非靜態(tài)資源文件夾中創(chuàng)建一個js文件
圖片.png
通過xhr來請求出json中的數(shù)據(jù)
// 定義配置變量
let config
// 定義同步獲取json文件數(shù)據(jù)方法
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的數(shù)據(jù),然后掛載到Vue.prototype上,同時把Vue實例也放在請求返回結(jié)果中(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()
})