elementui使用:default-active=“onRoutes“不起作用的原因

//html
<el-menu
        class="sidebar-el-menu"
        :default-active="onRoutes"
        :collapse="collapse"
        background-color="#334256"
        text-color="#ffffff"
        active-text-color="#20afff"
        router
    >

        <template v-for="item in items">
            <template>
                <el-menu-item :index="item.index" :key="item.index">
                    <i :class="item.icon"></i>
                    {{ item.title }}
                </el-menu-item>
            </template>
        </template>
    </el-menu>
//js zhong
data(){
        return {
            items:[
                {
                    icon: 'el-icon-document',
                    index: 'adIndex', //这个
                    title: '系统首页'
                },
                {
                    icon: 'el-icon-document',
                    index: 'adConsumer',
                    title: '用户管理'
                },
                {
                    icon: 'el-icon-document',
                    index: 'adChatroom',
                    title: '聊天房间管理'
                },
                {
                    icon: 'el-icon-document',
                    index: 'adArticle',
                    title: '文章管理'
                },
                {
                    icon: 'el-icon-document',
                    index: 'adReadtype',
                    title: '文章类别管理'
                },
                {
                    icon: 'el-icon-document',
                    index: 'adPsychologist',
                    title: '心理咨询师管理'
                },
                
                {
                    icon: 'el-icon-document',
                    index: 'adNotice',
                    title: '公告管理'
                },
                {
                    icon: 'el-icon-document',
                    index: 'adComment',
                    title: '评论管理'
                },
                {
                    icon: 'el-icon-document',
                    index: 'adFeedback',
                    title: '反馈管理'
                }
            ],
            // 是否折叠菜单
            collapse: false,
            // flag: "点我收起",
        }
    },
computed:{
        onRoutes(){
            console.log(this.$route.path.split('/'));
            return this.$route.path.split('/')[2]
        }
    }

正确的代码如上。

如下是错误的写法,原因为我的$route.path返回的路径是replace之后的为adhomeadIndex,而我的index写adIndex,两个路径不一样,所以浏览器解析时无法根据相同的路径去显示高亮

computed:{
        onRoutes(){
            console.log(this.$route.path.split('/'));
            return this.$route.path.replace('/','')
        }
    }

解决办法就是让onRoutes返回的数据和index显示的数据一样就可以。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值