雖然標題寫的是配置路由報錯,最終也是通過修改路由解決的,但是導致報錯的還有一個主要因素,是因為我增加了一個功能“頁面刷新時,根據url高亮左側導航”,如下圖:
1、頁面刷新,根據url高亮左側導航代碼如下:
1 // 刷新頁面時根據url高亮左側導航選項 2 highLightLeftNav() { 3 this.firstIndexCur = -1; 4 this.secondIndexCur = -1; 5 let pathName = this.$router.history.current.path; 6 this.customNav.forEach((item, index) => { 7 if(pathName.indexOf(item.pathUrl) !== -1) { // 高亮一級導航 8 this.firstIndexCur = index; 9 }else if(item.secondLevelNavList.length){ 10 item.secondLevelNavList.forEach((sonItem, sonIndex) => { // 高亮二級導航 11 if(pathName.indexOf(sonItem.pathUrl) !== -1) { 12 this.secondIndexCur = index + ',' + sonIndex; 13 } 14 }); 15 } 16 }); 17 }
(插個題外話,只有一級導航的時候高亮特別簡單用 :class=' { "active" ? currentIndex == index } ' 即可實現,而二級導航的時候就比較復雜了,具體如何實現可以看我的另一篇博客:vue高亮一級、二級導航)
轉回正題,看一下路由部分的代碼
2、路由部分代碼
個人理解之所以報錯就是因為頁面掛載完畢,如果按照配置路由選項默認高亮第一個,而按照①中刷新頁面高亮某個導航就不定了,此時既要高亮第一個,又要高亮其他的就會報錯