const BASE_URL = "http://localhost:8080"
export const myRequest = (options)=>{
return new Promise((resolve,reject)=>{
uni.request({
url:BASE_URL+options.url,
method:options.method || 'GET',
data:options.data || {},
success:(res)=>{
if(res.data.status!==0){
return uni.showToast({
title:'获取数据失败'
})
}
resolve(res)
},
fail:(err)=>{
uni.showToast({
title:'请求接口失败'
})
reject(err)
}
})
})
}
然后挂载到全局去使用:
即在main.js中进行如下编写
import {myRequest} from './util/api.js'
Vue.prototype.$myRequest = myRequest
页面中调用
async getSwipers(){
const res = await this.$myRequest({
url:'/api/getlunbo'
})
this.swipers = res.data.message
}