Vue路由切换 & Axios接口取消重复请求

210 篇文章 4 订阅
48 篇文章 2 订阅

在日常前端开发中, 经常会遇到频繁发起的重复请求, 会给服务器及网络造成不必要的压力, 可通过取消重复请求解决
场景

订单数据条件筛选查询

表单提交按钮频繁点击

路由页面切换请求未取消

解决方案:

在每个请求发起的时候存储当前存储的标记在一个数组或Map中, 针对每个请求(或者特定的请求,可以通过在请求头中加字段)的时候在请求拦截中查询是否重复, 如果已重复则取消历史中重复的请求, 再发起当前请求, 如果没有重复, 则添加存储标记并正常请求, 已请求完成的清除存储标记。

axios中如何取消请求

1、可以使用 CancelToken.source 工厂方法创建 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 参数是可选的)
source.cancel('Operation canceled by the user.');

还可以通过传递一个 executor 函数到 CancelToken 的构造函数来创建 cancel token:

const CancelToken = axios.CancelToken;
let cancel;
 
axios.get('/user/12345', {
  cancelToken: new CancelToken(function executor(c) {
    // executor 函数接收一个 cancel 函数作为参数
    cancel = c;
  })
});
 
// cancel the request
cancel();

项目中封装使用。


const pendingXHRMap = new Map();

/**
 * 添加请求
 * @param {Object} config
 */
export const addPendingXHR = (config) => {
  const url = [
    config.method,
    config.url
  ].join("&");
  config.cancelToken = config.cancelToken || new axios.CancelToken(cancel => {
    if (!pendingXHRMap.has(url)) { // 如果 pending 中不存在当前请求,则添加进去
      pendingXHRMap .set(url, cancel);
    }
  });
};

// 删除请求记录
/**
 * 移除请求
 * @param {Object} config;
 */
export const removePendingXHR = (config) => {
  const url = [
    config.method,
    config.url
  ].join("&");
  if (pendingXHRMap.has(url)) { // 如果在 pending 中存在当前请求标识,需要取消当前 请求,并且移除
    const cancel = pendingXHRMap.get(url);
    cancel(url);
    pendingXHRMap .delete(url);
  }
};

axios中使用:

// 请求拦截处理
axios.interceptors.request.use(config => {
    // 这里根据自定义的请求头进行判断
    removePendingXHR(config)
    addPendingXHR(config)
    ...
    return config
})
 
// 响应拦截处理
axios.interceptors.response.use(response => {
    removePendingXHR(response)
    ...
}, error => {
})

Vue中当路由切换页面的时候,将上一个页面的所有请求取消。

router.beforeEach((to, from, next) => {
    // 遍历pendingMap,将上一个页面的所有请求cancel掉
    pendingXHRMap.forEach((cancel) => {
        cancel();
    });
    pendingXHRMap.clear()
})
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值