当使用this.$router.pushthis.$router.repalce指令时报错“NavigationDuplicat: Avoided redundant navigation to current location“,即路由重复,主要是vue-router版本问题,有方法说将版本变为3.0,我尝试之后这种方法无效,经过尝试,以下两种方法有效。

方法1

在使用的指令后面加上.catch(err => err),即

this.$router.push(this.path).catch(err => err)
this.$router.replace(this.path).catch(err => err)

这种方法,需要在每次使用指令时都需要添加.catch(err => err)

方法2

修改push源码,在router下的index.js文件中添加下列代码即可。

// 解决vue-router在3.0版本以上重复报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

这种方法,只需要修改一次源码,使用指令时无需再添加.catch(err => err)

https://blog.csdn.net/weixin_43242112/article/details/107595460
https://www.cnblogs.com/yequxue/p/13255945.html

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐