解决 Vue 重复点击相同路由,出现 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation 问题

报错信息:Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location:

解决 Vue 重复点击相同路由,出现 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation 问题_第1张图片

 原因:使用编程式路由跳转(this.$router.push/replace)时,重复点击相同路由。

解决方案一:只需在 router 文件夹下的index.js中,添加如下代码:


//重写路由的原型对象身上的push方法
const VueRouterPush = VueRouter.prototype.push
VueRouter.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)
})

你可能感兴趣的:(VUE,vue.js,javascript,前端)