uniapp封装request请求(使用Promise封装)

一、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、使用说明

  • 在入口文件main.js中导入
  • 挂载在Vue原型上
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
			}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值