// 先保存vuerouter原型的push,来保存
let oriainPush = VueRouter.prototype.push;
let oriainReplace = VueRouter.prototype.replace;
// 重写push和replace方法
// 第一个参数 告诉原来的push方法,往哪里跳
VueRouter.prototype.push = function(location,resovle,reject){
if (resovle&&reject) {
// call 和apply 都可以调用函数一次,都可以篡改函数的 上下文
// call 传参数用,隔开 apply方法执行,传递数组
oriainPush.call(this,location,resovle,reject)
}else{
oriainPush.call(this,location,()=>{},()=>{})
}
}
// 重写replace方法
VueRouter.prototype.replace = function(location,resovle,reject){
if (resovle&&reject) {
// call 和apply 都可以调用函数一次,都可以篡改函数的 上下文
// call 传参数用,隔开 apply方法执行,传递数组
oriainReplace.call(this,location,resovle,reject)
}else{
oriainReplace.call(this,location,()=>{},()=>{})
}
}
重写push和replace方法
最新推荐文章于 2023-11-22 21:28:10 发布