Avoided redundant navigation to current location:'/xxx/xxx'多次操作跳转同一个路由报错

1,283 阅读1分钟

Avoided redundant navigation to current location:'/xxx/xxx'

产生原因:

token过期会跳转到login重新登录,如果发送了两个接口同时被response拦截后同时路由切换,就会报错。 还有点击两次路由切换(menu),多次点击跳转同一个路由同样是不被允许的。

这个问题是vue-router版本导致的问题,可以改变版本到3.0,也可以重写规则。

重写规则

改变原型链的push/replace方法(在引了vue-router的js文件里加上如下代码即可)

我的代码里push不管用,建议使用replace

import Vue from 'vue'  //如果已引用,不需要重复引用
import Router from 'vue-router'; //如果已引用,不需要重复引用
Vue.use(Router) //如果已引用,不需要重复引用

//push 
const VueRouterPush = Router.prototype.push 
Router.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
}

//replace
const VueRouterReplace = Router.prototype.replace
Router.prototype.replace = function replace (to) {
  return VueRouterReplace.call(this, to).catch(err => err)
}