vue函数式编程解决promise报错问题

问题

报错详情

出现原因

对于目前的VueRouter的版本而言,其底层时用的promise函数写的,而promise函数执行需要两个回调,一个成功的回调,和一个失败的回调。vue底层设计时声明式编程已经完善了promise的回调,而函数式编程并没有进行维护,此时当多次触发函数时就会出现上图错误

解决方法

重写pushreplace方法

1
2
3
4
5
6
7
8
9
10
11
12
13
//重写vueRouter原型对象的push方法,解决函数式编程时多次点击报错
//保存vueRoter原型对象的push方法,
let originPush = VueRouter.prototype.push;
//参数一,往哪里跳转(传递那些参数)
//参数二,成功回调
//参数三,失败回调
VueRouter.prototype.push = function (location, resolve, reject) {
if (resolve && reject) {
originPush.call(this, location, resolve, reject)
} else {
originPush.call(this, location, () => { }, () => { })
}
}
1
2
3
4
5
6
7
8
let originReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function (location, resolve, reject) {
if (resolve && reject) {
originReplace.call(this, location, resolve, reject)
} else {
originReplace.call(this, location, () => { }, () => { })
}
}

vue函数式编程解决promise报错问题
https://jessnzz.github.io/2021/12/13/vue函数式编程解决promise报错问题/
作者
Jessn
发布于
2021年12月13日
许可协议