push和replace返回的是一个promise
因为promise需要传递一个成功和失败的回调,所以我们要重写push和replace
在src下的router下的index.js
先把VueRouter原型对象上的push保存一份,防止覆盖成空
// 先把vuerouter原型对象的push,先保存一份
let originPush = VueRouter.prototype.push
// 重写push和replace方法
// location告诉原来的push方法,往哪里调传递那些参数
// resolve成功回调另一个失败的回调
VueRouter.prototype.push = function (location, resolve, reject) {
if (resolve && reject) {
originPush.call(this, location, resolve, reject)
} else {
originPush.call(this, location, () => { }, () => { })
}
}
// 重写push和replace方法
VueRouter.prototype.replace = function (location, resolve, reject) {
if (resolve && reject) {
originPush.call(this, location, resolve, reject)
} else {
originPush.call(this, location, () => { }, () => { })
}
}
这样在传递相同参数时就不会报出相关错误