Vue, Avoided redundant navigation to current location: "/login".

Vue Avoided redundant navigation to current location: "/login".

=================================

报错解释:
这个错误是在使用Vue.js框架时,发生的一个警告,表示尝试进行一个冗余的导航到当前位置(即“/login”路径)。这通常发生在Vue Router中,当你尝试通过编程方式导航到当前正在显示的路径时。

解决方法:
    检查你的路由跳转代码,确保不会不小心导航到当前位置。例如,避免在用户已经在"/login"页面时调用this.$router.push('/login')。
    可以使用this.$router.push之前先判断当前路径是否已经是目标路径,只有在不同路径时才进行跳转
    使用replace替换而不是push,这样可以避免在历史记录中创建新的记录,从而避免可能产生的冗余导航问题。

示例代码:
// 假设当前路径是/current-path
if (this.$route.path !== '/login') {
  this.$router.replace('/login');
}

=================================

if (this.$route.path !== '/login') {
  this.$router.push('/login')
}

=================================

Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/someurl".

 

 

解决方案:

在下面这个文件的末尾添加代码

 代码如下:

// 解决重复点击导航时,控制台出现报错
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

 

 

 

posted @ 2024-09-01 23:37  emanlee  阅读(688)  评论(0)    收藏  举报