VUE-Router之解决 Navigating to current location (XXX) is not allowed

261 阅读1分钟

我用的"vue-router"版本是"^3.3.2"

出现了这个问题,当你在当前路由下,再去push当前路由的时候,报了这样一个错误。

Navigating to current location (XXX) is not allowed

可能是高级版本做了限制,不允许这样做。

解决这个问题,有两个方法。

1.是版本降级

2.是重写push函数。

import Vue from 'vue';
import VueRouter from 'vue-router'; //导入路由
Vue.use(VueRouter);
//push
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

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