Vue路由切換和Axios接口取消重復請求詳解
前言
在日常前端開發(fā)中, 經(jīng)常會遇到頻繁發(fā)起的重復請求, 會給服務器及網(wǎng)絡造成不必要的壓力, 可通過取消重復請求解決
場景
- 訂單數(shù)據(jù)條件篩選查詢
- 表單提交按鈕頻繁點擊
- 路由頁面切換請求未取消
解決方案
在每個請求發(fā)起的時候存儲當前存儲的標記在一個數(shù)組或Map中, 針對每個請求的時候在請求攔截中查詢是否重復, 如果已重復則取消歷史中重復的請求, 再發(fā)起當前請求, 如果沒有重復, 則添加存儲標記并正常請求, 已請求完成的清除存儲標記
axios中如何取消請求
- 可以使用
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.');
- 還可以通過傳遞一個 executor 函數(shù)到 CancelToken 的構造函數(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();
項目中封裝使用
基本變量定義
// 是否取消重復請求開關 const cancelDuplicated = true // 存儲每個請求中的map const pendingXHRMap = new Map() // 取消請求類型定義 便于后期對此類型不做異常處理 const REQUEST_TYPE = { DUPLICATED_REQUEST: 'duplicatedRequest' }
設置重復標記的函數(shù)
const duplicatedKeyFn = (config) => { // 可在此設置用戶自定義其他唯一標識 默認按請求方式 + 請求地址 return `${config.method}${config.url}` }
添加到請求記錄
const addPendingXHR = (config) => { if (!cancelDuplicated) { return } const duplicatedKey = JSON.stringify({ duplicatedKey: duplicatedKeyFn(config), type: REQUEST_TYPE.DUPLICATED_REQUEST }) config.cancelToken = config.cancelToken || new axios.CancelToken((cancel) => { if (duplicatedKey && !pendingXHRMap.has(duplicatedKey)) { pendingXHRMap.set(duplicatedKey, cancel) } }) }
刪除請求記錄
const removePendingXHR = (config) => { if (!cancelDuplicated) { return } const duplicatedKey = JSON.stringify({ duplicatedKey: duplicatedKeyFn(config), type: REQUEST_TYPE.DUPLICATED_REQUEST }) if (duplicatedKey && pendingXHRMap.has(duplicatedKey)) { const cancel = pendingXHRMap.get(duplicatedKey) cancel(duplicatedKey) pendingXHRMap.delete(duplicatedKey) } }
axios中使用
// 請求攔截處理 axios.interceptors.request.use(config => { removePendingXHR(config) addPendingXHR(config) ... return config }) // 響應攔截處理 axios.interceptors.response.use(response => { removePendingXHR(response.config) ... }, error => { // 如果是取消請求類型則忽略異常處理 let isDuplicatedType; try { const errorType = (JSON.parse(error.message) || {}).type isDuplicatedType = errorType === REQUEST_TYPE.DUPLICATED_REQUEST; } catch (error) { isDuplicatedType = false } if (!isDuplicatedType) { // 其他異常處理 } })
Vue中當路由切換頁面的時候,將上一個頁面的所有請求取消
router.beforeEach((to, from, next) => { // 遍歷pendingMap,將上一個頁面的所有請求cancel掉 pendingXHRMap.forEach((cancel) => { cancel(); }); pendingXHRMap.clear() })
總結
本文主要介紹了在日常前端開發(fā)中, 遇到各種情況下頻繁發(fā)起的重復請求, 會給服務器及網(wǎng)絡造成不必要的壓力, 可通過取消重復請求解決。
到此這篇關于Vue路由切換和Axios接口取消重復請求的文章就介紹到這了,更多相關Vue Axios接口重復請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
參考
相關文章
vue使用keep-alive保持滾動條位置的實現(xiàn)方法
這篇文章主要介紹了vue使用keep-alive保持滾動條位置的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04Vue-cli 使用json server在本地模擬請求數(shù)據(jù)的示例代碼
本篇文章主要介紹了Vue-cli 使用json server在本地模擬請求數(shù)據(jù)的示例代碼,非常具有實用價值,需要的朋友可以參考下2017-11-11