uni-app 请求接口的封装
request.js
const Url = '127.0.0.1:8080'
export const request = (params)=>{
return new Promise((resolve,reject)=>{
wx.showLoading({ title: '加载中' })
uni.request({
url: Url + params.url,
method: params.methods || 'GET',
data:{},
success:res=>{
resolve(res)
uni.hideLoading()
},
fail:err=>{
uni.showToast({ title: '请求接口失败' })
reject(err)
uni.hideLoading()
},
})
})
}
导入main.js,挂在到vue的原型对象上
import { myRequest } from './util/api.js'
Vue.prototype.$myRuquest = myRequest
使用
async getDetailContent () {
const res = await this.$myRuquest({
url: '/api/goods/getdesc/'
})
},