跳到主内容

element-ui Avoided redundant navigation to current location 错误

· 1分钟阅读

问题

在使用 ElementUI 中的导航时,如果重复点击某选项,会报以下错误,虽然不会导致应用奔溃,但是有强迫症的我看起来很不舒服

element-ui.common.js?b705:3354 Error: Avoided redundant navigation to current location:/home”.
at createRouterError
at createNavigationDuplicatedError (vue-router.esm.js?8c4f:2033)
at HashHistory.confirmTransition (vue-router.esm.js?8c4f:2182)
at HashHistory.transitionTo (vue-router.esm.js?8c4f:2123)
at HashHistory.push (vue-router.esm.js?8c4f:2582)
at VueRouter.push (vue-router.esm.js?8c4f:2903)
at VueComponent.routeToItem (element-ui.common.js?b705:3381)
at VueComponent.handleItemClick (element-ui.common.js?b705:3348)
at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
at VueComponent.Vue.$emit (vue.runtime.esm.js?2b0e:3888)

解决

有两种解决方法

  1. 更新 vue-router 为 3.0 版本即可
npm i vue-router@3.0 -S
  1. src/router/index.js 文件里修改代码
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
return originalPush.call(this, location).catch((err) => err);
};

修改为下面的代码

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
return originalReplace.call(this, location).catch((err) => err);
};