解决编程式路由跳转,多次执行抛出NavigationDuplicated异常
![](https://img2023.cnblogs.com/blog/2111933/202303/2111933-20230318184334343-235451231.png)
解决:在vue-router的index.js中添加如下代码
//先保存router原型对象的push let originPush = VueRouter.prototype.push let originReplace = VueRouter.prototype.replace //第一个参数:告诉push方法往哪里跳转,并传递哪些参数 //第二个参数: 成功回调 //第三个参数:失败回调 VueRouter.prototype.push = function(location, resolve, reject) { if (resolve && reject) { //js中的每个函数都有call和apply方法,唯一区别就是call传参用逗号隔开,apply使用数组,相同点都可以调用函数一次 originPush.call(this, location, resolve, reject) } else { originPush.call(this, location, () => {}, () => {}) } } VueRouter.prototype.replace = function(location, resolve, reject) { if (resolve && reject) { //js中的每个函数都有call和apply方法,唯一区别就是call传参用逗号隔开,apply使用数组,相同点都可以调用函数一次 originReplace.call(this, location, resolve, reject) } else { originReplace.call(this, location, () => {}, () => {}) } }