用Axios Element實(shí)現(xiàn)全局的請求loading的方法
背景
業(yè)務(wù)需求是這樣子的,每當(dāng)發(fā)請求到后端時就觸發(fā)一個全屏的 loading,多個請求合并為一次 loading。
現(xiàn)在項目中用的是 vue 、axios、element等,所以文章主要是講如果使用 axios 和 element 實(shí)現(xiàn)這個功能。
效果如下:
分析
首先,請求開始的時候開始 loading, 然后在請求返回后結(jié)束 loading。重點(diǎn)就是要攔截請求和響應(yīng)。
然后,要解決多個請求合并為一次 loading。
最后,調(diào)用element 的 loading 組件即可。
攔截請求和響應(yīng)
axios 的基本使用方法不贅述。筆者在項目中使用 axios 是以創(chuàng)建實(shí)例的方式。
// 創(chuàng)建axios實(shí)例 const $ = axios.create({ baseURL: `${URL_PREFIX}`, timeout: 15000 })
然后再封裝 post 請求(以 post 為例)
export default { post: (url, data, config = { showLoading: true }) => $.post(url, data, config) }
axios 提供了請求攔截和響應(yīng)攔截的接口,每次請求都會調(diào)用showFullScreenLoading方法,每次響應(yīng)都會調(diào)用tryHideFullScreenLoading()方法
// 請求攔截器 $.interceptors.request.use((config) => { showFullScreenLoading() return config }, (error) => { return Promise.reject(error) }) // 響應(yīng)攔截器 $.interceptors.response.use((response) => { tryHideFullScreenLoading() return response }, (error) => { return Promise.reject(error) })
那么showFullScreenLoading tryHideFullScreenLoading()要干的事兒就是將同一時刻的請求合并。聲明一個變量needLoadingRequestCount,每次調(diào)用showFullScreenLoading方法 needLoadingRequestCount + 1。調(diào)用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount為 0 時,結(jié)束 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()就是調(diào)用 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() }
到這里,基本功能已經(jīng)實(shí)現(xiàn)了。每發(fā)一個 post 請求,都會顯示全屏 loading。同一時刻的多個請求合并為一次 loading,在所有響應(yīng)都返回后,結(jié)束 loading。
功能增強(qiáng)
實(shí)際上,現(xiàn)在的功能還差一點(diǎn)。如果某個請求不需要 loading 呢,那么發(fā)請求的時候加個 showLoading: false的參數(shù)就好了。在請求攔截和響應(yīng)攔截時判斷下該請求是否需要loading,需要 loading 再去調(diào)用showFullScreenLoading()方法即可。
在封裝 post 請求時,已經(jīng)在第三個參數(shù)加了 config 對象。config 里包含了 showloading。然后在攔截器中分別處理。
// 請求攔截器 $.interceptors.request.use((config) => { if (config.showLoading) { showFullScreenLoading() } return config }) // 響應(yīng)攔截器 $.interceptors.response.use((response) => { if (response.config.showLoading) { tryHideFullScreenLoading() } return response })
我們在調(diào)用 axios 時把 config 放在第三個參數(shù)中,axios 會直接把 showloading 放在請求攔截器的回調(diào)參數(shù)里,可以直接使用。在響應(yīng)攔截器中的回調(diào)參數(shù) response 中則是有一個 config 的 key。這個 config 則是和請求攔截器的回調(diào)參數(shù) config 一樣。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue項目使用localStorage+Vuex保存用戶登錄信息
這篇文章主要為大家詳細(xì)介紹了Vue項目使用localStorage+Vuex保存用戶登錄信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05element-ui upload組件上傳文件類型限制問題小結(jié)
最近我遇到這樣的問題,接受類型已經(jīng)加了accept 但是當(dāng)選擇彈出本地選擇文件時候切換到所有文件 之前的文件類型就本根過濾不掉了,下面小編給大家介紹element-ui upload組件上傳文件類型限制問題小結(jié),感興趣的朋友一起看看吧2024-02-02初學(xué)vue出現(xiàn)空格警告的原因及其解決方案
今天小編就為大家分享一篇初學(xué)vue出現(xiàn)空格警告的原因及其解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10