解决Vue报错Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location

今天写代码的时候碰到了个问题,报错如图所示

问题原因是:重复点击相同路由



有效的解决方法如下:

(亲测有效)方法一:在 router 文件夹下,添加如下代码:

Vue.use(Router)
const router = new Router({
  routes
})
 
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).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)
})

猜你喜欢

转载自blog.csdn.net/xx19960125/article/details/125117447