element侧边栏子路由点击不高亮问题

最近自己封装侧边栏 又碰到了点击子路由不高亮的问题

html 复制代码
<template>
    <div class="aside">
        <el-scrollbar :vertical="true" class="scrollbar_left_nav">
            <el-menu :default-active="defaultActive" :collapse="$store.state.isCollapse" background-color="#3D9A4A"
                text-color="#fff" active-text-color="#fff" :collapse-transition="false">
                <!-- 一级菜单 -->
                <div v-for="(item, index) in getMenu" :key="item.name">
                    <el-submenu :index="item.name" v-if="item.children && item.children.length && !item.meta.noShow">
                        <template slot="title">
                            <i class="el-icon-s-order"></i>
                            <span v-if="!$store.state.isCollapse">{{ item.meta.title }}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <div v-for="(nth, idx) in item.children" :key="nth.name">
                            <el-submenu :index="nth.name" v-if="nth.children && nth.children.length">
                                <template slot="title">
                                    <i class="el-icon-s-order"></i>
                                    <span>{{ nth.meta.title }}</span>
                                </template>
                                <!-- 三级菜单 -->
                                <div v-for="(lth, ids) in nth.children" :key="lth.name">
                                    <el-submenu :index="lth.name" v-if="lth.children && lth.children.length">
                                        <template slot="title">
                                            <i class="el-icon-s-order"></i>
                                            <span>{{ lth.meta.title }}</span>
                                        </template>
                                        <!-- 四级菜单 -->
                                        <el-menu-item :index="ath.name" v-for="(ath, ida) in lth.children" :key="ath.name"
                                            @click="goRouter(ath)">
                                            <i class="el-icon-s-order"></i>
                                            <span>{{ ath.meta.title }}</span>
                                        </el-menu-item>
                                    </el-submenu>
                                    <el-menu-item v-else :index="lth.name" @click="goRouter(lth)">
                                        <i class="el-icon-s-order"></i>
                                        {{ lth.meta.title }}
                                    </el-menu-item>
                                </div>
                            </el-submenu>
                            <el-menu-item v-else :index="nth.name" @click="goRouter(nth)">
                                <i class="el-icon-s-order"></i>
                                {{ nth.meta.title }}
                            </el-menu-item>
                        </div>
                    </el-submenu>
                    <el-menu-item v-else :index="item.children[0].name" @click="goRouter(item.children[0])">
                        <i class="el-icon-s-order"></i>
                        <span v-if="!$store.state.isCollapse">{{ item.children[0].meta.title }}</span>
                    </el-menu-item>
                </div>
            </el-menu>
        </el-scrollbar>

    </div>
</template>
 
<script>
export default {
    name: 'layout-sidebar',
    data() {
        return {
            defaultActive: 'bigShow', // 当前高亮
        }
    },
    computed: {
        // 显示在侧边栏的路由
        getMenu() {
            return this.$router.options.routes.filter(
                it => !it.meta.noLayout && it.path.search(this.$store.state.nowRouter) != -1
            )
        }
    },
    watch:{
        $route(){
            this.defaultActive = this.$route.name
        }
    },
    methods: {
        // 点击侧边栏 路由跳转事件
        goRouter(data) {
            this.defaultActive = data.name
            // 判断一下 点击跳转页面的路由是否是当前路由  如果是则不做反应  否则跳转到点击页
            if (data.name !== this.$route.name) {
                // 路由跳转
                this.$router.push({
                    name: data.name
                })
            }

        },
    }
}
</script>
 
<style scoped>
::v-deep .el-scrollbar__wrap{
    overflow: hidden !important;
    overflow-y: auto !important;
}
.scrollbar_left_nav {
    height: calc(100vh - 100px);
    overflow-x: hidden !important;
    overflow-y: auto !important;
}

.aside {
    height: 100%;
    width: 100%;
    position: relative;
    padding-bottom: 20px;
}

::v-deep .is-active {
    background-color: #30863B !important;
}
</style>

原因:因为我路由渲染使用的是 v-for ,他要绑定 key 值,key 值我喜欢绑**(item,index)in xx** 里的index 就这块出现了问题,改成唯一值就好了

相关推荐
程序员agions4 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发4 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_4 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞054 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、4 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao4 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly4 小时前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强
一 乐5 小时前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
科技D人生5 小时前
Vue.js 学习总结(20)—— Vue-Office 实战:word、pdf、excel、ppt 多种文档的在线预览
vue.js·word·vue-pdf·stylesheet·docx-preview·vue-office
vx1_Biye_Design5 小时前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven