问题情景:
做登录功能时,点击登录按钮,触发this.$router.push()方法进行路由跳转
只点击一次按钮时可以正常跳转
连续点击多次时会报错:
Avoided redundant navigation to current location: "/welcome".
原因:
多次点击时路由发生了重复跳转,就会报错
解决:
第一种:路由版本的原因,将vue-router降级到3.0.x版本就不会出现这个问题
第二种:
以修改代码的方式,修改vue-router原型对象中的push方法,捕获错误,就不会出现报错
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}