vue项目:侧边栏搭配路由侧边栏高亮

  1. 如果项目中使用的是element-ui
    可以直接使用它的NavMenu组件实现导航栏高亮,它会在activeIndex字段自动识别,匹配到下面item的index,给它加上高亮效果,所以我选择item循环的时候,index绑定每个字段的路由地址,在computed中计算当前路由前两个字段,并赋值给activeIndex,这样可以保证刷新保持高亮效果
<el-menu :default-active="activeIndex"
                 mode="horizontal"
                 background-color="#2a2f3b"
                 text-color="#fff"
                 active-text-color="#2B8EE7"
                 :router="routerOpen"
        >
            <el-menu-item class="logo-img">
                <template>
                    <img src="@/assets/img/nav/logo.png" @click="gotoRainbow"/>
                </template>
            </el-menu-item>          
</el-menu>


// computed中
 computed: {
        activeIndex() {
            const routePath = this.$route.path;
            return routePath.split('/').slice(0, 3).join('/');
        },
      }
  1. 还可以使用路由配置meta
// 相关路由中
{
        name: 'IMP',
        path: '/main/sectorDetails/IMP',
        component: resolve => require(['@/views/sectorDetails/buList/IMP'], resolve),
        meta: {
            typeId: '1',
        }
    }


// 导航栏组件中读取meta中的值,

created() {
        this.defaultActive = this.$route.meta.typeId;
    },

然后用上面相同的设置就好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值