阿修罗
阿修罗
  • 发布:2021-11-10 18:54
  • 更新:2021-11-10 18:54
  • 阅读:711

nuve vue3 封装接口请求

分类:nvue

http.js

function http(url, method='get', data){
uni.showLoading({mask:true});
return new Promise((resolve, reject) => {
uni.request({
url:'http://aichat.jucd.cn/api/'+ url,
method: method,
header:{
token:uni.getStorageSync('token'),
},
success: (res) => {
resolve(res);
},
fail:(res) => {
uni.showModal({
content: '网络联接失败',
showCancel: false
});
reject(err)
},
complete: (res) => {
uni.hideLoading();
}
})
})

return url + getApp().globalData.apiroot  + uni.getStorageSync('token');  

}
export default http;

main.js
import http from './common/http.js'
app.config.globalProperties.$http = http;

页面调用

getApp().$http('get/base').then(res => {
this.http_val = res;
console.log(res.statusCode)
})

0 关注 分享

要回复文章请先登录注册