国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

axios簡單實現小程序延時loading指示

2019-11-19 13:23:05
字體:
來源:轉載
供稿:網友

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 = 0loadingStatus$.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,請告訴我)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 峨边| 饶平县| 类乌齐县| 马龙县| 惠东县| 集贤县| 家居| 略阳县| 平武县| 淳安县| 文成县| 墨竹工卡县| 涟水县| 奎屯市| 五台县| 通河县| 江油市| 屏山县| 伊川县| 阳泉市| 玛曲县| 新丰县| 彩票| 海阳市| 图木舒克市| 贺州市| 湘西| 南安市| 类乌齐县| 平定县| 游戏| 禄丰县| 象州县| 高密市| 阿巴嘎旗| 佛教| 丁青县| 辽阳市| 广丰县| 田阳县| 崇阳县|