1. // 删除重复请求
    2. let s = 1; //单位秒,ajax取消延后时间
    3. let pending = []; //声明一个数组用于存储每个请求的取消函数和axios标识
    4. let cancelToken = Axios.CancelToken;
    5. let removePending = config => {
    6. pending = pending.filter(i => new Date().getTime() - i.t < 500); // 过滤n秒前发的请求
    7. if (
    8. pending.length > 1 &&
    9. pending[pending.length - 2].u === config.url &&
    10. pending[pending.length - 1].t - pending[pending.length - 2].t < s * 500
    11. ) {
    12. // console.log('ajax取消操作');
    13. pending[pending.length - 2].f(); //执行取消操作
    14. pending.splice(pending.length - 2, 1);
    15. }
    16. };

    请求拦截器内:

    const CANCEL_RETRANSMIT = [] // 指定需判断重复请求的接口
    
    axios.interceptors.request.use(
      async config => {
          ...
    
        // 删除重复请求
        if (CANCEL_RETRANSMIT.indexOf(config.url) > -1) {
          console.log(config);
          config.cancelToken = new cancelToken(c => {
            pending.push({
              u: url,
              t: new Date().getTime(),
              f: c,
            });
          });
          await removePending(config);
        }
      }
    
      return config;
     }