解决编程式导航push点击多次出现的bug

在router.js里书写代码

 1 let originPush = VueRouter.prototype.push;
 2 let originReplace = VueRouter.prototype.replace;
 3 //VueRouter.prototype原型对象添加一个方法
 4 //location:路由跳转相关的信息
 5 VueRouter.prototype.push = function (location, resolve, reject) {
 6     //当前函数this:即为VueRouter类的实例
 7     //相当于push方法里面this,是windows【完犊子了】
 8     //利用人家push方法实现路由跳转,保证push里面this,应该vueRouter类的实例
 9 
10     //面试:函数apply与call区别?
11     //相同的地方:都可以篡改函数里面this
12     //不同的地方:apply传递参数 数组  call传递参数 逗号分割
13 
14     if (resolve && reject) {
15         //代表真:代表着两个形参接受参数【箭头函数】
16         originPush.call(this, location, resolve, reject);
17     } else {
18         originPush.call(this, location, () => { }, () => { });
19     }
20 }
21 VueRouter.prototype.replace = function (location, resolve, reject) {
22     if (resolve && reject) {
23         //代表真:代表着两个形参接受参数【箭头函数】
24         originReplace.call(this, location, resolve, reject);
25     } else {
26         originReplace.call(this, location, () => { }, () => { });
27     }
28 }

 

posted @ 2022-10-24 09:56  小闫的姑娘  阅读(38)  评论(0编辑  收藏  举报