亲宝软件园·资讯

展开

ElementUI导航栏重复点菜单报错

酷酷的橙007 人气:0

ElementUI 导航栏重复点菜单报错

在使用ElementUI中的导航时,默认情况下如果重复点击某选项,会报错。

element-ui.common.js?b705:3354 Error: Avoided redundant navigation to current location: “/home/home1”.
at createRouterError (vue-router.esm.js?8c4f:2060)
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)

如图所示:

可以在router的配置文件中(router -> index.js)加上下面这句话,注意位置:

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

问题即可解决。

ElementUI 菜单导航重定向报错处理

当我们使用ElementUI中的菜单导航时,配置好菜单路由后,重复点击同一个菜单项会报错,如下:

Error: Avoided redundant navigation to current location: “/xxx”.

还有一个错误是,我配置了一个路由守卫,当用户还没有登录的时候,点击菜单项时,如果没有登录则会跳转至登录页面进行登录,这时页面可以正常跳转但是也会报出错误,如下:

Error: Redirected when going from “/xxx” to “/yyy” via a navigation guard.

这个问题困扰了我两天,查了一些资料也没有解决,直到看到一个文章,写的是解决第一个问题的方法,如下:

import VueRouter from 'vue-router'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

在引入vue-router的地方加入此代码块,就能解决重复点击同一菜单项的报错,但是神奇的是,我的第二个问题也因此解决了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论