出现原因:在使用编程式声明时,Vue 重复点击相同路由造成的,在Vue3中不会出现
解决方法:在router中,添加一下代码:
//完整形式
let originPush = VueRouter.prototype.push;
let originRelace = VueRouter.prototype.replace;
VueRouter.prototype.push = function(location,resolve,reject){
if(resolve&&reject){
originPush.call(this,location,resolve,reject)
}else{
originPush.call(this,location,()=>{},()=>{})
}
}
VueRouter.prototype.replace = function(location,resolve,reject){
if(resolve&&reject){
originRelace.call(this,location,resolve,reject)
}else{
originRelace.call(this,location,()=>{},()=>{})
}
}
//简写
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}
const originalRelace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
return originalRelace.call(this, location).catch(err => err)
}