一、uniapp封装request请求
- BASE_URL为服务器请求地址
- uni.request(OBJECT) 发起网络请求
- uni.showToast(OBJECT) 显示消息提示框
1、具体代码如下
const BASE_URL = 'http://47.95.12.147:8082'
export const myRequest = (options) => {
return new Promise((reslove, 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: "数据获取失败"
})
}
reslove(res)
},
fail:(err)=> {
uni.showToast({
title: "请求接口失败"
})
reject(err)
}
})
})
}
2、使用说明
import { myRequest } from './util/api.js'
Vue.prototype.$myRequest=myRequest
async getSwipers() {
const res = await this.$myRequest({
url: '/api/getlunbo'
})
//data中使用swiperArr存储获取到的数据
this.swiperArr = res.data.message
}