如何解決ElementUI導(dǎo)航欄重復(fù)點菜單報錯問題
ElementUI 導(dǎo)航欄重復(fù)點菜單報錯
在使用ElementUI中的導(dǎo)航時,默認(rèn)情況下如果重復(fù)點擊某選項,會報錯。
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導(dǎo)航欄中的vue-router在3.0版本以上重復(fù)點菜單報錯問題
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}問題即可解決。
ElementUI 菜單導(dǎo)航重定向報錯處理
當(dāng)我們使用ElementUI中的菜單導(dǎo)航時,配置好菜單路由后,重復(fù)點擊同一個菜單項會報錯,如下:
Error: Avoided redundant navigation to current location: “/xxx”.
還有一個錯誤是,我配置了一個路由守衛(wèi),當(dāng)用戶還沒有登錄的時候,點擊菜單項時,如果沒有登錄則會跳轉(zhuǎn)至登錄頁面進行登錄,這時頁面可以正常跳轉(zhuǎn)但是也會報出錯誤,如下:
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的地方加入此代碼塊,就能解決重復(fù)點擊同一菜單項的報錯,但是神奇的是,我的第二個問題也因此解決了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Vue3與免費滿血版DeepSeek實現(xiàn)無限滾動+懶加載+瀑布流模塊及優(yōu)化過程
在進行非完全標(biāo)準(zhǔn)化數(shù)據(jù)的可視化展示時,瀑布流是一種經(jīng)常被采用的展示方法,瀑布流能夠有效地將不同大小規(guī)格的內(nèi)容以一種相對規(guī)整的方式呈現(xiàn)出來,本文給大家介紹了基于Vue3與免費滿血版DeepSeek實現(xiàn)無限滾動+懶加載+瀑布流模塊,需要的朋友可以參考下2025-03-03
Vue動態(tài)獲取數(shù)據(jù)后控件不可編輯問題
這篇文章主要介紹了Vue動態(tài)獲取數(shù)據(jù)后控件不可編輯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
element-ui自定義message-box自定義樣式不生效的解決
這篇文章主要介紹了element-ui自定義message-box自定義樣式不生效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

