vue axios攔截器常用之重復(fù)請求取消
引言
上一篇介紹了axios的簡單封裝,知道了axios攔截器的應(yīng)用場景和方法,今天來看一下對于響應(yīng)時間過長且請求次數(shù)過高的情況攔截器如何處理。
取消請求的方法
Axios使用內(nèi)部提供的CancelToken來取消請求
官網(wǎng)示例1:用CancelToken.source工廠方法創(chuàng)建 cancel token,像這樣
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
axios.get('/user/12345', {
cancelToken: source.token
}).catch(function(thrown) {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
} else {
// 處理錯誤
}
});
axios.post('/user/12345', {
name: 'new name'
}, {
cancelToken: source.token
})
// 取消請求(message 參數(shù)是可選的)
source.cancel('Operation canceled by the user.');
官網(wǎng)示例2:通過傳遞一個 executor 函數(shù)到 CancelToken 的構(gòu)造函數(shù)來創(chuàng)建 cancel token:
const CancelToken = axios.CancelToken;
let cancel;
axios.get('/user/12345', {
cancelToken: new CancelToken(function executor(c) {
// executor 函數(shù)接收一個 cancel 函數(shù)作為參數(shù)
cancel = c;
})
});
// cancel the request
cancel();
可以看到上面都是在單個請求中創(chuàng)建的 cancel token 實際工作中我們需要對所有的請求都進行處理,接下來我們看下如何在攔截器實現(xiàn)取消請求的功能
攔截器中取消重復(fù)請求
import axios from 'axios'
import baseURL from './config'
import qs from 'qs'
const pendingRequest = new Map(); // 請求對象
const CancelToken = axios.CancelToken;
axios.defaults.timeout = 30000
axios.defaults.baseURL = baseURL.target
// 添加請求攔截器
axios.interceptors.request.use(function(config) {
// 在發(fā)送請求之前做些什么
config.headers = {
'content-type': 'application/json',
'token': getToken()
}
// 獲取請求key
let requestKey = getReqKey(config);
// 判斷是否是重復(fù)請求
if (pendingRequest.has(requestKey)) { // 是重復(fù)請求
removeReqKey(requestKey); // 取消
}else{
// 設(shè)置cancelToken
config.cancelToken = new CancelToken(function executor(cancel) {
pendingRequest.set(requestKey, cancel); // 設(shè)置
})
}
return config;
}, function (error) {
// 請求錯誤
return Promise.reject(error);
});
// 添加響應(yīng)攔截器
axios.interceptors.response.use(function (response) {
// 請求對象中刪除requestKey
let requestKey = getReqKey(response.config);
removeReqKey(requestKey);
// 對返回數(shù)據(jù)做點啥 比如狀態(tài)進行攔截
if (response.data.status !== 200) {
Toast({
message: response.data.message,
type: 'warning',
duration: 1000
})
return
}
// 沒問題 返回服務(wù)器數(shù)據(jù)
return response.data;
}, function (error) {
let requestKey = getReqKey(error.config);
removeReqKey(requestKey);
// 響應(yīng)錯誤
return Promise.reject(error);
});
// 獲取請求key
function getReqKey(config) {
// 請求方式、請求地址、請求參數(shù)生成的字符串來作為是否重復(fù)請求的依據(jù)
const { method, url, params, data } = config; // 解構(gòu)出來這些參數(shù)
// GET ---> params POST ---> data
const requestKey = [ method, url, qs.stringify(params), qs.stringify(data)].join('&');
return requestKey;
}
// 取消重復(fù)請求
function removeReqKey(key) {
const cancelToken = pendingRequest.get(key);
cancelToken(key); // 取消之前發(fā)送的請求
pendingRequest.delete(key); // 請求對象中刪除requestKey
}
結(jié)語
以上就是對重復(fù)請求的處理,如果對攔截器不清楚的可以看下上篇文章,有問題歡迎大家提出指正,我會在第一時間更新。
到此這篇關(guān)于vue axios攔截器常用之重復(fù)請求取消的文章就介紹到這了,更多相關(guān)axios攔截器重復(fù)請求取消內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中調(diào)用api接口實現(xiàn)數(shù)據(jù)的渲染以及詳情方式
這篇文章主要介紹了vue3中調(diào)用api接口實現(xiàn)數(shù)據(jù)的渲染以及詳情方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
element tree懶加載:load="loadNode"只觸發(fā)一次的解決方案
本文主要介紹了element tree懶加載:load="loadNode"只觸發(fā)一次的解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-08-08
vue中Echarts使用動態(tài)數(shù)據(jù)的兩種實現(xiàn)方式
這篇文章主要介紹了vue中Echarts使用動態(tài)數(shù)據(jù)的兩種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

