直接上干货

在发送第二次请求的时候如果第一次请求还未返回,则取消第一次请求,以保证后发送的请求返回的数据不会被先发送的请求覆盖。

或者是跳转路由的时候取消还未返回的请求

第一步: axios 怎么取消:

const CancelToken = axios.CancelToken;
let cancel;
axios.get('/user/12345', {
cancelToken: new CancelToken(function executor(c) {
// An executor function receives a cancel function as a parameter
cancel = c;
})
}); 第二步: 这是取消一个axios 如果是多个呢?那么我们就需要有个地方给存下来
vuex
state: {
// axios取消请求
axiosPromiseCancel: [],
}
第三步: axios 拦截器axios.js
import store from './store';
const CancelToken = axios.CancelToken; let cancel;
// 防止重复请求
let removePending = (config) => {
for (let k in store.state['axiosPromiseCancel']) {
if (store.state['axiosPromiseCancel'][k].u === config.url + '&' + config.method) { //当前请求在数组中存在时执行函数体
store.state['axiosPromiseCancel'][k].f(); //执行取消操作
store.state['axiosPromiseCancel'].splice(k, 1); //把这条记录从数组中移除
}
}
};
axios.interceptors.request.use(config=>{
// 这个是 取消重点
removePending(config);
config.cancelToken = new CancelToken((cancel) => {
store.state['axiosPromiseCancel'].push({ u: config.url + '&' + config.method, f: cancel });
});
  return config;
});
axios.interceptors.response.use(res=>{
removePending(res.config);
  // do something...
},error=>{
if (axios.isCancel(error)) {
// 为了终结promise链 就是实际请求 不会走到.catch(rej=>{});这样就不会触发错误提示之类了。
return new Promise(() => {});
}else{
return Promise.reject(error)
}});

第四步: 就是在router 里做取消动作了 router.js

import store from './store';
router.beforeEach((to, from, next) => {
if(store.state['axiosPromiseCancel'].length > 0) {
store.state['axiosPromiseCancel'].forEach(e => {
e && e.f()
});
}
store.state['axiosPromiseCancel'] = [];
})

vue axios路由跳转取消所有请求 和 防止重复请求的更多相关文章

  1. vue路由跳转取消上个页面的请求和去掉重复请求

    vue路由跳转取消上个页面的请求和去掉重复请求 axios 的二次封装(拦截重复请求.异常统一处理) axios里面拦截重复请求

  2. Vue.之.路由跳转

    Vue.之.路由跳转 在进行项目开发的过程中,需要使用路由进行跳转.如下: // 不带有参数,在页面上跳转到别的页面 1. this.$router.push('/login/init');  // ...

  3. Vue中断axios请求-切换页面+重复请求

    切换页面时中断 一.概述 在Vue单页面开发过程中,遇到这样的情况,当我切换页面时,由于上一页面请求执行时间长,切换到该页面时,还未执行完,这时那个请求仍会继续执行直到请求结束,此时将会影响页面性能, ...

  4. Vue之路由跳转 传参 aixos 和cookie

    一.路由跳转 1.1 项目的初始化 vue create m-proj   >>>创建vue项目 精简vue项目的 views 视图   About(基本是删除的) Home.(可以 ...

  5. vue设置路由跳转参数,以及接收参数

    最近做Vue项目,遇到了一个路由跳转问题:首页要跳转到项目页指定的Tab选项卡项,一开始总是跳到默认项.解决方法如下: 在跳转链接处设置了路由跳转参数,如下: <router-link  :to ...

  6. vue二级路由跳转后外部引入js失效问题解决方案

    vue路由可以通过children嵌套,于是可以形成二级路由等等... 案例如下: routes: [ { path: '/', name: 'dy', component: dy, children ...

  7. vue当前路由跳转初步研究

    一样闲话少说,直接上问题,如图: 也是消息面板,没想到一个小小的消息面板,碰到这么多坑,惆怅. 就是如果当前路由和跳转路由不一样时,正常跳转没有任何问题.但是如果一样时,就不会跳转了,用了很多方法,比 ...

  8. vue -- router路由跳转错误 , NavigationDuplicated

    vue-router ≥3.0版本回调形式以及改成promise api的形式了,返回的是一个promise,如果路由地址跳转相同, 且没有捕获到错误,控制台始终会出现如图所示的警告 (注:3.0以下 ...

  9. vue router路由跳转了,但是页面没有变(已解决)

    小白学习 router.js:两个组件之间跳转 但是路由变了,页面没有改变的原因是因为app.vue里面没有router-view(很关键)

随机推荐

  1. 本地连接服务器的redis,jedisCluster创建问题

    1.redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 查看 ...

  2. PP: Data-driven classification of residential energy consumption patterns by means of functional connectivity networks

    Purpose Implement a good user aggregation and classification. or to assess the interrelation pattern ...

  3. 显示当前目录命令 - pwd

    1) 命令名称:pwd 2) 英文原意:print working directory 3) 命令所在路径:/bin/pwd 4) 执行权限:所有用户 5) 功能描述:显示当前目录 6) 语法: pw ...

  4. python3练习100题——017

    原题链接:http://www.runoob.com/python/python-exercise-example17.html 题目:输入一行字符,分别统计出其中 英文字母.空格.数字和其它字符的个 ...

  5. 使用鼠标左键事件实现VR中的Eye Gaze Input

    1.光标以及光标动画的显示 using System.Collections; using System.Collections.Generic; using UnityEngine; using U ...

  6. 关于Win32串口

    因为近段时间接触Hid相对来说多一些,由此忽略了串口中获取cbInQue这个重要的东西,下面是错误代码 // Win32SerialPortLib.cpp : 定义 DLL 应用程序的导出函数. // ...

  7. AcWing 154. 滑动窗口

    https://www.acwing.com/problem/content/156/ #include <iostream> using namespace std; ; int a[N ...

  8. cookie不支持中文,必须转码后存储,否则会乱码

    cookie不支持中文,必须转码后存储,否则会乱码 Cookie ck = new Cookie("username", URLEncoder.encode(name, " ...

  9. 题解【洛谷P4588】[TJOI2018]数学计算

    题目描述 小豆现在有一个数\(x\),初始值为\(1\).小豆有\(Q\)次操作,操作有两种类型: \(1\;m\):\(x=x\times m\)输出\(x\%mod\); \(2\;pos\):\ ...

  10. 洛谷 P1659 [国家集训队]拉拉队排练(Manacher)

    题目链接:https://www.luogu.com.cn/problem/P1659 思路: 首先跑一遍Manacher,用$cnt_i$记录长为$i$的回文串有多少个. 所记录的$cnt$并不是最 ...