溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么解決ElementUI導航欄重復點菜單報錯問題

發(fā)布時間:2022-08-01 09:27:02 來源:億速云 閱讀:168 作者:iii 欄目:開發(fā)技術

本文小編為大家詳細介紹“怎么解決ElementUI導航欄重復點菜單報錯問題”,內容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“怎么解決ElementUI導航欄重復點菜單報錯問題”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

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)

如圖所示:

怎么解決ElementUI導航欄重復點菜單報錯問題

可以在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”.

還有一個錯誤是,我配置了一個路由守衛(wèi),當用戶還沒有登錄的時候,點擊菜單項時,如果沒有登錄則會跳轉至登錄頁面進行登錄,這時頁面可以正常跳轉但是也會報出錯誤,如下:

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的地方加入此代碼塊,就能解決重復點擊同一菜單項的報錯,但是神奇的是,我的第二個問題也因此解決了。

讀到這里,這篇“怎么解決ElementUI導航欄重復點菜單報錯問題”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI