用Axios Element 實現全局的請求 loading


 
 
Kapture 2018-06-07 at 14.57.40.gif

demo in github

背景

業務需求是這樣子的,每當發請求到后端時就觸發一個全屏的 loading,多個請求合並為一次 loading。
現在項目中用的是 vue 、axios、element等,所以文章主要是講如果使用 axios 和 element 實現這個功能。

分析

首先,請求開始的時候開始 loading, 然后在請求返回后結束 loading。重點就是要攔截請求和響應。
然后,要解決多個請求合並為一次 loading。
最后,調用element 的 loading 組件即可。

攔截請求和響應

axios 的基本使用方法不贅述。筆者在項目中使用 axios 是以創建實例的方式。

// 創建axios實例 const $ = axios.create({ baseURL: `${URL_PREFIX}`, timeout: 15000 }) 

然后再封裝 post 請求(以 post 為例)

export default { post: (url, data, config = { showLoading: true }) => $.post(url, data, config) } 

axios 提供了請求攔截和響應攔截的接口,每次請求都會調用showFullScreenLoading方法,每次響應都會調用tryHideFullScreenLoading()方法

// 請求攔截器 $.interceptors.request.use((config) => { showFullScreenLoading() return config }, (error) => { return Promise.reject(error) }) // 響應攔截器 $.interceptors.response.use((response) => { tryHideFullScreenLoading() return response }, (error) => { return Promise.reject(error) }) 

那么showFullScreenLoading tryHideFullScreenLoading()要干的事兒就是將同一時刻的請求合並。聲明一個變量needLoadingRequestCount,每次調用showFullScreenLoading方法 needLoadingRequestCount + 1。調用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1needLoadingRequestCount為 0 時,結束 loading。

let needLoadingRequestCount = 0 export function showFullScreenLoading() { if (needLoadingRequestCount === 0) { startLoading() } needLoadingRequestCount++ } export function tryHideFullScreenLoading() { if (needLoadingRequestCount <= 0) return needLoadingRequestCount-- if (needLoadingRequestCount === 0) { endLoading() } } 

startLoading()endLoading()就是調用 element 的 loading 方法

import { Loading } from 'element-ui' let loading function startLoading() { loading = Loading.service({ lock: true, text: '加載中……', background: 'rgba(0, 0, 0, 0.7)' }) } function endLoading() { loading.close() } 

到這里,基本功能已經實現了。每發一個 post 請求,都會顯示全屏 loading。同一時刻的多個請求合並為一次 loading,在所有響應都返回后,結束 loading。

功能增強

實際上,現在的功能還差一點。如果某個請求不需要 loading 呢,那么發請求的時候加個 showLoading: false的參數就好了。在請求攔截和響應攔截時判斷下該請求是否需要loading,需要 loading 再去調用showFullScreenLoading()方法即可。

在封裝 post 請求時,已經在第三個參數加了 config 對象。config 里包含了 showloading。然后在攔截器中分別處理。

// 請求攔截器 $.interceptors.request.use((config) => { if (config.showLoading) { showFullScreenLoading() } return config }) // 響應攔截器 $.interceptors.response.use((response) => { if (response.config.showLoading) { tryHideFullScreenLoading() } return response }) 

我們在調用 axios 時把 config 放在第三個參數中,axios 會直接把 showloading 放在請求攔截器的回調參數里,可以直接使用。在響應攔截器中的回調參數 response 中則是有一個 config 的 key。這個 config 則是和請求攔截器的回調參數 config 一樣。


更新:2018-6-7

合並一定間隔時間內請求的 loading

上面的代碼已經實現了將有時間重合的 loading 合並,什么意思呢?請看下圖


 
image.png

在 request1 的 loading 還沒結束時,request2 的 loading 已經開始。這種情況 request1 和 request2 在時間上有一定的重合,所以 loading 可以合並。

那么 request3 是在 request2 結束后 100ms 開始 loading.這時你會發現 loading 兩次,並且中間有一次極短的閃爍,這當然是很不好的體驗了。

我們只需要修改 tryHideFullScreenLoading 即可:

export function tryHideFullScreenLoading() { if (needLoadingRequestCount <= 0) return needLoadingRequestCount-- if (needLoadingRequestCount === 0) { _.debounce(tryCloseLoading, 300)() } } const tryCloseLoading = () => { if (needLoadingRequestCount === 0) { loading.close() } } 

在之前的版本中,tryHideFullScreenLoading 方法會判斷 needLoadingRequestCount === 0 立即關閉 loading。現在使用 lodash. debounce,延遲 300ms 再調用 tryCloseLoading 方法。

現在 300ms 間隔內的 loading 也就合並為一次啦……

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM