axios简单实现小程序延时loading指示
小程序和小游戏的wx.showLoading方法相信大家都不会陌生,但是怎样处理loading才能又更好的用户体验呢?
假设需求如下,1秒类请求没有相应,才弹出loading,否则不弹出,请求错误时,弹出toast。
配合axios实现如下:
1.在状态管理部分存储loading状态
export const loadingStatus$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false) axios.interceptors.request.use( (config: any) => { loadingStatus$.next(true) return config }, (error: any) => { return Promise.reject(error) }, ) axios.interceptors.response.use( (response: any) => { loadingStatus$.next(false) return response.data }, (error: any) => { loadingStatus$.next(false) wx.showToast({ title: 'something wrong happened, please try it later' }) return Promise.reject(error) }, )
2.在应用启动时订阅
let timer: any = 0 loadingStatus$ .pipe( pairwise(), filter((res: Array<boolean>) => { if (res[0] !== res[1]) { return true } else { return false } }), map((res: Array<boolean>) => { return res[1] }), ) .subscribe((res: boolean) => { // once changed, value must be distinct if (timer === 0) { timer = setTimeout(() => { wx.showLoading({ title: 'loading...' }) }, 1000) } else { clearTimeout(timer) timer=0 wx.hideLoading() } })
感觉配合rx,很多复杂功能都能很简单地实现,另外这个功能会伴随整个应用周期,所以unsbscribe可以不用太在意。(除非有其他的bad effect,请告诉我)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无争山庄资源网 Copyright www.whwtcm.com
暂无“axios简单实现小程序延时loading指示”评论...
更新日志
2024年10月09日
2024年10月09日
- 【原神手游】5.1版本更新维护预告
- 李锦华.1987-日与夜(LP版)【SOLO】【WAV+CUE】
- 伍珂玥.2014-伍珂玥【湾彩星辰】【FLAC分轨】
- 吕方.2001-旧情人·老情歌精选2CD【华纳】【WAV+CUE】
- 《星际战士2》¥219,《100% 鲜橙汁》¥7
- 手办鉴赏室:少女眼神颇有杀意!连衣裙勾勒傲人轮廓
- 未来可期的二次元大逃杀游戏——《白厄战境》简评
- 炉石传说死亡骑士卡组怎么搭配 死亡骑士最新卡组2024推荐
- 魔兽世界地下堡珍玩怎么升级 地下堡珍玩升级方法攻略分享
- 魔兽世界怎么获得泽克维尔的残血珍玩 泽克维尔的残血珍玩获取方式
- FrankSinatra-StrangersInTheNight(1966Jazz)[Flac24-96]
- 大原樱子《CAMON+5thAnniversaryBest~》2CD[24-96]flac
- Isgaard2003-GoldenKey(LimitedEdition)【WAV+CUE】
- 《合金装备3RE》伊娃定妆照公布:没被政确太好了
- 石家庄新增《黑神话:悟空》墙绘:小狐狸萍萍