api.js
const BASE_URL="http://api-hmugo-web.itheima.net/" //定义常量方便后期切换
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)=>{
console.log(res);
if(res.data.meta.status !== 200){
return uni.showToast({
title:"获取数据失败!"
})
}
resolve(res);
},
fail: (err) => {
uni.showToast({
title:"请求失败!"
})
}
})
})
}
main.js
import {myRequest} from './util/api.js'
Vue.prototype.$myRequest = myRequest
调用
export default {
data() {
return {
title: 'Hello',
imgArr:[]
}
},
onLoad() {
this.getSwipers();
// console.log("获取数据");
// uni.showToast({
// "title":"获取成功!"
// })
},
methods: {
async getSwipers(){
const res = await this.$myRequest({
"url":"api/public/v1/home/swiperdata"
});
console.log(res);
this.imgArr = res.data.message;
}
}
}