在uniapp中使用fly.js精简版实例,某朋友教我的

846次阅读
没有评论

共计 759 个字符,预计需要花费 2 分钟才能阅读完成。

// ajax
var Fly = require('./plugins/tools/fly.js');
var fly = new Fly
//添加请求拦截器
fly.interceptors.request.use((config,promise)=>{
if (!config.body.noLoading) {
// #ifndef APP-PLUS
uni.showLoading({
// title:'loading...',
mask: false
})
// #endif
// #ifdef APP-PLUS
plus.nativeUI.showWaiting('');
// #endif
}	
    return config;
})

//添加响应拦截器,响应拦截器会在then/catch处理之前执行
fly.interceptors.response.use(
    (response,promise) => {
        //只将请求结果的data字段返回
// #ifndef APP-PLUS
uni.hideLoading()
// #endif
// #ifdef APP-PLUS
plus.nativeUI.closeWaiting();
// #endif
        return response.data
    },
    (err,promise) => {
        //发生网络错误后会走到这里
        //promise.resolve("ssss")
// #ifndef APP-PLUS
uni.showToast({
title: '网络连接出现问题,请刷新或稍后再试',
icon: 'none',
mask: true,
duration: 2000
});
// #endif

// #ifdef APP-PLUS
plus.nativeUI.toast('网络连接出现问题,请刷新或稍后再试');
// #endif
    }
)
Vue.prototype.$request = fly;
正文完
 0
Eric chan
版权声明:本站原创文章,由 Eric chan 于2019-05-09发表,共计759字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。