溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Axios Element如何實(shí)現(xiàn)全局請(qǐng)求loading

發(fā)布時(shí)間:2021-07-06 11:05:16 來源:億速云 閱讀:200 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)使用Axios Element如何實(shí)現(xiàn)全局請(qǐng)求loading的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

背景

業(yè)務(wù)需求是這樣子的,每當(dāng)發(fā)請(qǐng)求到后端時(shí)就觸發(fā)一個(gè)全屏的 loading,多個(gè)請(qǐng)求合并為一次 loading。

現(xiàn)在項(xiàng)目中用的是 vue 、axios、element等,所以文章主要是講如果使用 axios 和 element 實(shí)現(xiàn)這個(gè)功能。

效果如下:

使用Axios Element如何實(shí)現(xiàn)全局請(qǐng)求loading

分析

首先,請(qǐng)求開始的時(shí)候開始 loading, 然后在請(qǐng)求返回后結(jié)束 loading。重點(diǎn)就是要攔截請(qǐng)求和響應(yīng)。

然后,要解決多個(gè)請(qǐng)求合并為一次 loading。

最后,調(diào)用element 的 loading 組件即可。

攔截請(qǐng)求和響應(yīng)

axios 的基本使用方法不贅述。筆者在項(xiàng)目中使用 axios 是以創(chuàng)建實(shí)例的方式。

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

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

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

axios 提供了請(qǐng)求攔截和響應(yīng)攔截的接口,每次請(qǐng)求都會(huì)調(diào)用showFullScreenLoading方法,每次響應(yīng)都會(huì)調(diào)用tryHideFullScreenLoading()方法

// 請(qǐng)求攔截器
$.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()要干的事兒就是將同一時(shí)刻的請(qǐng)求合并。聲明一個(gè)變量needLoadingRequestCount,每次調(diào)用showFullScreenLoading方法 needLoadingRequestCount + 1。調(diào)用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount為 0 時(shí),結(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ā)一個(gè) post 請(qǐng)求,都會(huì)顯示全屏 loading。同一時(shí)刻的多個(gè)請(qǐng)求合并為一次 loading,在所有響應(yīng)都返回后,結(jié)束 loading。

功能增強(qiáng)

實(shí)際上,現(xiàn)在的功能還差一點(diǎn)。如果某個(gè)請(qǐng)求不需要 loading 呢,那么發(fā)請(qǐng)求的時(shí)候加個(gè)  showLoading: false的參數(shù)就好了。在請(qǐng)求攔截和響應(yīng)攔截時(shí)判斷下該請(qǐng)求是否需要loading,需要 loading 再去調(diào)用showFullScreenLoading()方法即可。

在封裝 post 請(qǐng)求時(shí),已經(jīng)在第三個(gè)參數(shù)加了 config 對(duì)象。config 里包含了 showloading。然后在攔截器中分別處理。

// 請(qǐng)求攔截器
$.interceptors.request.use((config) => {
 if (config.showLoading) {
  showFullScreenLoading()
 }
 return config
})

// 響應(yīng)攔截器
$.interceptors.response.use((response) => {
 if (response.config.showLoading) {
  tryHideFullScreenLoading()
 }
 return response
})

我們?cè)谡{(diào)用 axios 時(shí)把 config 放在第三個(gè)參數(shù)中,axios 會(huì)直接把 showloading 放在請(qǐng)求攔截器的回調(diào)參數(shù)里,可以直接使用。在響應(yīng)攔截器中的回調(diào)參數(shù) response 中則是有一個(gè) config 的 key。這個(gè) config 則是和請(qǐng)求攔截器的回調(diào)參數(shù) config 一樣。

感謝各位的閱讀!關(guān)于“使用Axios Element如何實(shí)現(xiàn)全局請(qǐng)求loading”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI