使用vue開發過程中有的項目會存在多級導航的情況,如下圖,這種就存在了兩層,那么該如何高亮一級導航,又該如何高亮二級導航這就是今天我要記錄的內容。
1、高亮一級導航很簡單,代碼如下:
// 點擊一級導航 changeFirstLevel(index,e) { this.secondIndexCur = -1; this.firstIndexCur = index; }
2、高亮二級導航,代碼如下:
// view部分 <ul class="firstLevelNav"> <li v-for="(item, index) in customNav" :key="index"> <router-link :to="item.pathUrl"> <div :class="{'cur': firstIndexCur == index}" @click="changeFirstLevel(index, $event)">{{item.name}}</div> </router-link> <ul class="secondLevelNav"> <li v-for="(sonItem, sonIndex) in item.secondLevelNavList" :key="sonIndex"> <router-link :to="sonItem.pathUrl"> <div :class="[secondIndexCur == index + ',' + sonIndex ? 'cur' : '']" @click="changeSecondLevel(index, sonIndex, $event)">{{sonItem.name}}</div> </router-link> </li> </ul> </li> </ul> // 點擊二級導航 changeSecondLevel(index, sonIndex, e) { this.firstIndexCur = -1; this.secondIndexCur = index + ',' + sonIndex; }
(注意:為什么在<router-link></router-link>中又寫了一個div呢,是因為我在完成代碼后點擊導航高亮樣式並不能成功添加,開始我是直接把class添加到了rouer-link上)
以下是導航組件的全部代碼:

1 <template> 2 <!-- 自定義報表 - 左側導航 --> 3 <div class="customNav"> 4 <div class="pic"> 5 <img src="./images/pic.png" alt=""> 6 </div> 7 <ul class="firstLevelNav"> 8 <li v-for="(item, index) in customNav" :key="index"> 9 <router-link :to="item.pathUrl"> 10 <div :class="{'cur': firstIndexCur == index}" @click="changeFirstLevel(index, $event)">{{item.name}}</div> 11 </router-link> 12 <ul class="secondLevelNav"> 13 <li v-for="(sonItem, sonIndex) in item.secondLevelNavList" :key="sonIndex"> 14 <router-link :to="sonItem.pathUrl"> 15 <div :class="[secondIndexCur == index + ',' + sonIndex ? 'cur' : '']" @click="changeSecondLevel(index, sonIndex, $event)">{{sonItem.name}}</div> 16 </router-link> 17 </li> 18 </ul> 19 </li> 20 </ul> 21 </div> 22 </template> 23 24 <script> 25 export default { 26 data() { 27 return { 28 firstIndexCur: -1, // 一級導航添加類名元素 29 secondIndexCur: -1, // 一級導航添加類名元素 30 customNav: [{ 31 name: '數據源', 32 pathUrl: '/dataSource', 33 secondLevelNavList: [{ 34 name: '新建', 35 pathUrl: '/newlyBuild/addData' 36 }] 37 },{ 38 name: '配置SQL', 39 pathUrl: '/configSQL', 40 secondLevelNavList: [{ 41 name: '新建', 42 pathUrl: '/configNewlyBuild/addData' 43 }] 44 },{ 45 name: '業務SQL', 46 pathUrl: '/businessSQL', 47 secondLevelNavList: [] 48 }], 49 pathName: '' 50 } 51 }, 52 mounted() { 53 this.pathName = this.$router.history.current.path; 54 this.pathName = this.pathName === '/' ? '/dataSource' : this.pathName; // 默認路由 55 this.highLightLeftNav(); 56 }, 57 watch: { 58 // 監聽 url 的變化 59 $route(to, from) { 60 this.pathName = to.path; 61 this.highLightLeftNav(); 62 } 63 }, 64 methods: { 65 // 點擊一級導航 66 changeFirstLevel(index,e) { 67 this.secondIndexCur = -1; 68 this.firstIndexCur = index; 69 }, 70 // 點擊二級導航 71 changeSecondLevel(index, sonIndex, e) { 72 this.firstIndexCur = -1; 73 this.secondIndexCur = index + ',' + sonIndex; 74 }, 75 // 刷新頁面時根據url高亮左側導航選項 76 highLightLeftNav() { 77 this.firstIndexCur = -1; 78 this.secondIndexCur = -1; 79 this.customNav.forEach((item, index) => { 80 if(this.pathName.indexOf(item.pathUrl) !== -1) { 81 this.firstIndexCur = index; 82 return; 83 }else if(item.secondLevelNavList.length){ 84 item.secondLevelNavList.forEach((sonItem, sonIndex) => { 85 if(this.pathName.indexOf(sonItem.pathUrl) !== -1) { 86 this.secondIndexCur = index + ',' + sonIndex; 87 return; 88 } 89 }); 90 } 91 }); 92 }, 93 } 94 } 95 </script> 96 97 <style lang="scss" type="text/css" scoped>