前言
在开发中由于域名会经常变化,或者对借口和返回状态统一管理,这时候需要对uni的request进行二次封装
const BASE_URL='https://....'
export const myRequest=(option)=>{
return new Promise((resolve,reject)=>{
uni.request({
url:BASE_URL+options.url,
method:option.method||'GET',
data:option.data||{},
success:res=>{
if(re.data.status!==1){
return uni.showToast({
title:'获取数据失败'
})
}
resolve(res)
},
fail:(err)=> {
uni.showToast({
title:'请求借口失败'
})
reject(err)
}
})
})
}
main.js里面引用,并挂载到vue原型对象上
import { meyRequest} from './util/api.js'
vue.prototype.$myRequest=meyRequest
在组件中使用:
async getSwiper() {
const res=await this.$myRequest({
url:'xxx'//你的借口
})
console.log(res)
}