const BASE_URL = 'http://122.112.253.28:8082'
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)
}
})
})## import {myRequest} from './util/api.js'
Vue.prototype.request=myRequest//main.js文件导入
}
uni-app api文件 request
最新推荐文章于 2024-07-25 16:21:45 发布
该博客介绍了如何在uni-app中进行网络请求的封装,通过`myRequest`函数实现GET和POST请求,并在请求成功或失败时显示提示。在`main.js`中将请求方法挂载到Vue原型上,便于全局使用。
摘要由CSDN通过智能技术生成