elementUI 中MENU菜单踩坑

本文介绍如何在ElementUI中设置菜单自动激活当前页面选项,通过将路由与default-active绑定,实现非点击导航时自动更新选中状态,适用于博客或网站导航栏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

在写博客的时候用到了elementUI中menu菜单,

需求:点击当前页面的按钮跳转到首页,给menu中绑定的default-active赋值

问题:页面已经跳转过去,可menu选中项根本没有发生变化

解决办法: 直接将当前页面的路由绑定到default-active上,同时将index改为当前路由,这样在通过非点击导航菜单跳转页面时就不需要再来手动改变导航菜单的选中项了,它会自己选中当前页面的tab项,完美解决!(其实不止element-ui有这个问题,iview也是一样的)

// :default-active="$route.name"
<el-menu
        :router="true"
        :default-active="$route.name"
        class="el-menu-demo"
        mode="horizontal"
        @select="select"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
            <el-menu-item :route="{name: 'home'}" index="home">
                <template slot="title">
                    <Icon type="ios-home"></Icon>
                    <span>首页</span>
                </template>
            </el-menu-item>
            
            <el-menu-item :route="{name: 'article'}" index="article">
                <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>文章</span>
                </template>
            </el-menu-item>

            <el-menu-item :route="{name: 'time'}" index="time">
                <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>时间轴</span>
                </template>
            </el-menu-item>
            
            <el-menu-item :route="{name: 'photo'}" index="photo">
                <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>生活照</span>
                </template>
            </el-menu-item>
            
            <el-menu-item :route="{name: 'footprint'}" index="footprint">
                <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>足迹</span>
                </template>
            </el-menu-item>
            
            <el-menu-item :route="{name: 'aboutme'}" index="aboutme">
                <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>关于我</span>
                </template>
            </el-menu-item>

            <el-menu-item :route="{name: 'message'}" index="message">
                <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>留言板</span>
                </template>
            </el-menu-item>
            <el-menu-item :route="{name: 'manage'}" index="manage">
                <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>后台管理</span>
                </template>
            </el-menu-item>
    </el-menu>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值