解決elementui中NavMenu導(dǎo)航菜單高亮問題(解決多種情況)
前言
如果使用elementui這個框架來開發(fā)系統(tǒng),那么我們幾乎都會使用到NavMenu 組件。
我們可能需要菜單根據(jù)我們點(diǎn)擊的那一項(xiàng)高亮顯示,刷新頁面了之后還是會相應(yīng)的高亮顯示。
elementUI組件官網(wǎng):
https://element.eleme.io/#/zh-CN/component/menu
我在項(xiàng)目中遇到的情況有三種:(vue-cli+elementui)
1、橫向菜單
如果僅有一個菜單,那么點(diǎn)擊當(dāng)前菜單高亮;如果菜單下有二級下拉,那么點(diǎn)擊下拉二級菜單高亮,并且一級菜單也高亮,mode="horizontal"時一級菜單就會自動高亮了。(刷新頁面后依舊保持高亮)


2、縱向菜單
如果僅有一個菜單,那么點(diǎn)擊當(dāng)前菜單高亮;如果菜單下有二級下拉,那么點(diǎn)擊下拉二級菜單高亮(刷新頁面后依舊保持高亮)

3、嵌套式菜單
橫向主菜單高亮,代表這個頁面是當(dāng)前頁面;在當(dāng)前頁面中還嵌入一個縱向菜單,這個縱向菜單高亮代表這個頁面顯示不同的tab(刷新頁面后依舊保持高亮)


代碼實(shí)現(xiàn)
NavMenu 導(dǎo)航菜單高亮如果需要刷新頁面了之后也能高亮,那么就使用路由匹配式高亮。
我們的el-menu組件標(biāo)簽中配置最重要的兩個(其他配置看官網(wǎng)):
:default-active="activeMenu" // 默認(rèn)高亮(當(dāng)前激活菜單的 index) router // 是否使用 vue-router 的模式,啟用該模式會在激活導(dǎo)航時以 index 作為 path 進(jìn)行路由跳轉(zhuǎn),此時菜單的高亮是menu組件中的index屬性和地址欄中的路徑匹配到了就會高亮。
1、橫向菜單
導(dǎo)航菜單默認(rèn)為垂直模式,通過mode屬性設(shè)置成horizontal可以使導(dǎo)航菜單變更為水平模式。
<el-menu
:default-active="activeMenu"
class="el-menu-demo"
mode="horizontal"
router
:background-color="#000"
text-color="#fff"
active-text-color="#409eff">
<el-menu-item index="/home"><i class="iconfont icon-zhuye" style="vertical-align: 0px;"></i> 首頁</el-menu-item>
<el-submenu index="1">
<template slot="title"><i class="el-icon-monitor"></i>監(jiān)控</template>
<el-menu-item index="/monitor/aaa">111111</el-menu-item>
<el-menu-item index="/monitor/bbb">22222</el-menu-item>
<el-menu-item index="/monitor/ccc">333333</el-menu-item>
<el-menu-item index="/monitor/ddd">44444</el-menu-item>
</el-submenu>
<el-submenu index="2">
<template slot="title"><i class="el-icon-s-marketing"></i>CDN</template>
<el-menu-item index="/cdn/aaa">域名管理</el-menu-item>
<el-menu-item index="/cdn/bbb">111111</el-menu-item>
<el-menu-item index="/cdn/ccc">222222</el-menu-item>
<el-menu-item index="/cdn/ddd">333333</el-menu-item>
</el-submenu>
<el-menu-item index="/apiDocument"><i class="iconfont icon-iconset0118"></i> 接口文檔</el-menu-item>
</el-menu>
export default {
computed: {
// 我們使用計算屬性來獲取到當(dāng)前點(diǎn)擊的菜單的路由路徑,然后設(shè)置default-active中的值
// 使得菜單在載入時就能對應(yīng)高亮
activeMenu() {
const route = this.$route
const { meta, path } = route
// if set path, the sidebar will highlight the path you set
// 可以在路由配置文件中設(shè)置自定義的路由路徑到meta.activeMenu屬性中,來控制菜單自定義高亮顯示
if (meta.activeMenu) {
return meta.activeMenu
}
return path
}
}
}2、縱向菜單
其實(shí)橫向菜單和縱向菜單的配置差不多,區(qū)別在于去掉mode屬性就變成縱向垂直菜單了。高亮配置和橫向菜單是一致的。
在縱向菜單的時候如果點(diǎn)擊二級菜單高亮了,默認(rèn)一級菜單不會高亮。
3、嵌套式菜單
嵌套式菜單中我們要同時控制兩個分離菜單的高亮,那么在路由配置文件router/index.js中及其重要了,需要在每項(xiàng)路由中設(shè)置meta屬性的不同字段來控制高亮。
記住:刷新仍能高亮是以路由路徑匹配式高亮
結(jié)合第一點(diǎn)的橫向菜單為主菜單menu配置,特別注意文檔接口那一項(xiàng)的index路徑。
文檔接口頁面的縱向菜單配置:
<el-menu
:default-active="activeMenu"
class="el-menu-vertical-demo api-menu"
router
@open="handleOpen"
@close="handleClose"
background-color="#f8f8f8"
:default-openeds=defaultOpen
text-color="#666"
active-text-color="#409eff">
<el-submenu index="1">
<template slot="title">
<i class="el-icon-s-marketing"></i>
<span class="bold-font">CDN</span>
</template>
<el-menu-item index="/apiDocument/cdnSummary">
<span class="bold-font">概覽</span>
</el-menu-item>
<el-submenu index="1-1">
<template class="sub-drop-title" slot="title"><span class="bold-font">API列表</span> 總數(shù)2個</template>
<el-menu-item class="api-menu-item" index="/apiDocument/pushRefresh">
<span class="api-item">
<span>push_refresh </span>
<span style="font-size: 13px;color:#999;">aaaaa</span>
</span>
</el-menu-item>
<el-menu-item class="api-menu-item" index="/apiDocument/refreshTasks">
<span class="api-item">
<span>refresh_tasks </span>
<span style="font-size: 13px;color:#999;">bbbbb</span>
</span>
</el-menu-item>
</el-submenu>
</el-submenu>
</el-menu>
export default {
data () {
return {
defaultOpen: ['1','1-1'] // 默認(rèn)展開菜單項(xiàng)
}
},
computed: {
activeMenu() {
const route = this.$route
const { meta, path } = route
// if set path, the sidebar will highlight the path you set
if (meta.apiActiveMenu) { // 注意這里很重要
return meta.apiActiveMenu
}
return path
}
}
}
router/index.js文件的路由配置(特別關(guān)注activeMenu、apiActiveMenu的配置):
routes: [
// 重定向,打開接口文檔頁面就重定向到 概覽
{
path: '/apiDocument',
redirect: '/apiDocument/cdnSummary',
hidden: true // 不需要再M(fèi)enu菜單上顯示的就設(shè)置這個屬性
},
// 接口文檔路由
{
// 接口文檔菜單的動態(tài)加載配置 start
path: '',
name: 'apiDocument',
component: customerLayout,
gotoPath: '/apiDocument', // 自定義屬性 (主菜單中的index屬性值)
meta: {
title: '接口文檔',
activeMenu: '/apiDocument'
},
showOne: true, // 自定義屬性
hidden: true,
// 接口文檔菜單的動態(tài)加載配置 end
children: [{
path: '',
component: apiDoc,
children: [
{
path: '/apiDocument/cdnSummary',
component: cdnSummary,
meta: {
activeMenu: '/apiDocument', // 主菜單 的 接口文檔 高亮
apiActiveMenu: '/apiDocument/cdnSummary' // 接口文檔的子菜單高亮
}
},
{
path: '/apiDocument/pushRefresh',
component: pushRefresh,
meta: {
activeMenu: '/apiDocument',
apiActiveMenu: '/apiDocument/pushRefresh' // 接口文檔的子菜單高亮
}
},
{
path: '/apiDocument/refreshTasks',
component: refreshTasks,
meta: {
activeMenu: '/apiDocument',
apiActiveMenu: '/apiDocument/refreshTasks' // 接口文檔的子菜單高亮
}
}
]
}]
}
]
實(shí)現(xiàn)高亮的思想是:
在每次點(diǎn)擊縱向菜單時瀏覽器地址欄中的地址就會改變,這時地址欄就會和apiActiveMenu配置中地址一致,我們使用apiActiveMenu來自定義控制二級對應(yīng)高亮;
而一級菜單欄由activeMenu來自定義高亮,使得activeMenu配置和一級菜單的index屬性中的配置一直一致,這樣接口文檔內(nèi)的縱向菜單在切換的時候就不會影響到一級主菜單的高亮了。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹
這篇文章主要介紹了Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
Vue實(shí)現(xiàn)Chrome小恐龍游戲的示例代碼
Google 給 Chrome 瀏覽器加了一個有趣的彩蛋,本文就詳細(xì)的介紹一下Vue實(shí)現(xiàn)Chrome小恐龍游戲的示例代碼,具有一定的參考價值,感興趣的可以了解一下2022-04-04
vue獲取token(設(shè)置token,清除token)實(shí)現(xiàn)登錄方式
這篇文章主要介紹了vue獲取token(設(shè)置token,清除token)實(shí)現(xiàn)登錄方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue在chrome44偶現(xiàn)點(diǎn)擊子元素事件無法冒泡的解決方法
這篇文章主要給大家介紹了關(guān)于Vue在chrome44偶現(xiàn)點(diǎn)擊子元素事件無法冒泡的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

