解决vue-router报NavigationDuplicated: Avoided redundant navigation to current location 的问题

最近写项目的时候, 重复点击路由会在控制台报这样的错误。

image

它的提示是 避免到当前位置的冗余导航。 简单来说就是重复触发了同一个路由。
这个错误是 vur-router更新以后新出现的错误。(我使用的是 vue-router 3.2.0)出现的
解决这个错误也非常简单。只需要在router /index.js 的页面里面 加入

const originalPush = VueRouter.prototype.push

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

这行代码就可以了
他的位置大概在这里
image

posted @ 2021-12-22 10:56  DeyouKong  阅读(1142)  评论(0编辑  收藏  举报