vue重复点击路由报错,解决NavigationDuplicated: Avoided redundant navigation to current location: 问题
vue项目中重复加载当前路由会报错,解决方法:找到项目中的router文件,在里面添加代码,修正Vue原型上的push和replace方法(注意vue-router的引入名称)// 缓存原型上的push函数const originPush = Router.prototype.pushconst originReplace = Router.prototype.replace// 给原型对象上的p
·
vue项目中重复加载当前路由会报错,

解决方法:找到项目中的router文件,在里面添加代码,修正Vue原型上的push和replace方法
(注意vue-router的引入名称)
// 缓存原型上的push函数
const originPush = Router.prototype.push
const originReplace = Router.prototype.replace
// 给原型对象上的push指定新函数函数
Router.prototype.push = function (location, onComplete, onAbort) {
// 判断如果没有指定回调函数, 通过call调用源函数并使用catch来处理错误
if (onComplete===undefined && onAbort===undefined) {
return originPush.call(this, location, onComplete, onAbort).catch(() => {})
} else { // 如果有指定任意回调函数, 通过call调用源push函数处理
originPush.call(this, location, onComplete, onAbort)
}
}
// replace函数
Router.prototype.replace = function (location, onComplete, onAbort) {
if (onComplete===undefined && onAbort===undefined) {
return originReplace.call(this, location, onComplete, onAbort).catch(() => {})
} else {
originReplace.call(this, location, onComplete, onAbort)
}
}
可以按需添加,当时我遇到这个问题按照百度上搜索的办法没有解决的原因是因为我项目中用的是replace方法,可是百度上都是修改push方法,导致不生效,现在列出两种push和replace方法的修改代码,看自己用的是哪种方法,按需添加就生效了
更多推荐




所有评论(0)