如何解決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)
如圖所示:
可以在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的地方加入此代碼塊,就能解決重復點擊同一菜單項的報錯,但是神奇的是,我的第二個問題也因此解決了。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue element-ui使用required進行表單校驗時自定義提示語問題
這篇文章主要介紹了vue element-ui使用required進行表單校驗時自定義提示語問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05vue把頁面轉換成圖片導出方式(html2canvas導出不全問題)
這篇文章主要介紹了vue把頁面轉換成圖片導出方式(html2canvas導出不全問題),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10淺談Vue static 靜態(tài)資源路徑 和 style問題
這篇文章主要介紹了淺談Vue static 靜態(tài)資源路徑 和 style問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11