import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
Vue.prototype.request= ({url,type='get',data={},headers={},options={},success,fail=null,complate=null})=>{
if(type.toLowerCase()==='get'){
data={
params:data
}
axios.get(url,data,{
…header,…options
}).then(res=>{
success(res.data); //在函数里使用this 要用箭头函数,要不然会导致this指向问题
}).catch(error=>{
fail?fail(error):null;
}).finally(()=>{
complate?complate():null;
})
}
if(type.toLowerCase()==='post'){
if(!data.constructor===FormData){
data=qs.stringify(data);
}
axios.post(url,data,{
…headers,…options
}).then(res=>{
success(res.data);
}).catch(error=>{
fail?fail(error):null;
}).finally(()=>{
complate?complate():null;
})
}
}
再次封装axios
最新推荐文章于 2024-09-30 14:28:41 发布