$router.push()或者$router.replace 重复点击的时候会报错,这个错误老早之前也是没有的,后来router更新了一次有了
为什么会报这个错误,为了防止重复渲染组件,抛出来的一个错误
那么如何解决?
1. 在使用 $router.push() 的时候做处理
$router.push()
.then((result) => {}).catch((err) => {
});
可以使用catch捕获到这个错误,不打印即可
一般不用第一种解决办法,为什么?
因为不可能每次写 $router.push() 去catch
// 2. 重写push方法即可(在router.js中)
//重写push方法
const originPush = VueRouter.prototype.push; // 把官方的push方法暂存到originPush这个变量里面
VueRouter.prototype.push = function push (location) { // 参数是调用push方法传进来的参数
return originPush.call(this, location).catch(() => {});
}
-------------------------------------------
//或者重写replace方法
const originReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace (location) {
return originReplace.call(this, location).catch(() => {})
}