axios簡單實(shí)現(xiàn)小程序延時(shí)loading指示
axios簡單實(shí)現(xiàn)小程序延時(shí)loading指示
小程序和小游戲的wx.showLoading方法相信大家都不會陌生,但是怎樣處理loading才能又更好的用戶體驗(yàn)?zāi)兀?/p>
假設(shè)需求如下,1秒類請求沒有相應(yīng),才彈出loading,否則不彈出,請求錯(cuò)誤時(shí),彈出toast。
配合axios實(shí)現(xiàn)如下:
1.在狀態(tài)管理部分存儲loading狀態(tài)
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.在應(yīng)用啟動時(shí)訂閱
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,很多復(fù)雜功能都能很簡單地實(shí)現(xiàn),另外這個(gè)功能會伴隨整個(gè)應(yīng)用周期,所以unsbscribe可以不用太在意。(除非有其他的bad effect,請告訴我)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Vue中使用this.$store或者是$route一直報(bào)錯(cuò)的解決
今天小編就為大家分享一篇在Vue中使用this.$store或者是$route一直報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue實(shí)現(xiàn)模態(tài)框的通用寫法推薦
下面小編就為大家分享一篇vue實(shí)現(xiàn)模態(tài)框的通用寫法推薦,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02使用Vue實(shí)現(xiàn)一個(gè)樹組件的示例
這篇文章主要介紹了使用Vue實(shí)現(xiàn)一個(gè)樹組件的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-11-11vue-cli history模式實(shí)現(xiàn)tomcat部署報(bào)404的解決方式
這篇文章主要介紹了vue-cli history模式實(shí)現(xiàn)tomcat部署報(bào)404的解決方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09vue-router之實(shí)現(xiàn)導(dǎo)航切換過渡動畫效果
今天小編就為大家分享一篇vue-router之實(shí)現(xiàn)導(dǎo)航切換過渡動畫效果,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10