Vue经典报错---重复点击相同路由

2,638 阅读1分钟

在写vue时可能会经常遇到这个报错信息:


Uncaught (in promise) NavigationDuplicated: 
Avoided redundant navigation to current location:

这算是一个比较经典的报错了,属于路由重复点击引起的(即自己跳转自己),在路由配置文件中几行代码就可以解决了(几乎是固定写法,能记住更好,记不住的可以收藏一波,用的时候粘贴就可以了),代码如下:

const originaPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originaPush.call(this, location).catch(err => err)
}

当然也有其他解决方案:

在跳转时,判断跳转路由和当前路由是否一致,不一致再跳转,从而避免重复跳转产生问题

toMenu (item){
  if(this.$route.path !==item.url){
  this.$router.push({path:item.url})
  }
}

使用catch方法捕获router.push异常错误

this.$router.push(route).catch(err=>{
  console.log(err)
})