使用el-menu-item点击页面不高亮

文章讲述了在Vue项目中,如何解决el-menu由于动态路由导致的默认激活项高亮问题,关键在于将default-active的值设置为完整的$router.fullPath,确保菜单项与路由参数匹配,无论是否有查询参数都能正确高亮。

标题所指的情况有很多种,我找过的很多答案要么是没有给el-menu加router,或者是menu-item没有绑定正确的index。而我这里给出的情况是:当我循环列表生成el-menu-item,他们之间使用id区分(或者是其他参数)。

如果el-menu的default-active="$route.name",因为他们的route.name相同,所以点击任意一项,el-menu-item会全部高亮。如果用$route.path,就会出现第一次点击不高亮,只有处于当前页面时,再次点击才会高亮。这时因为$route.path后面是没有参数的,这个值不等于el-menu-item的index(index是这个页面对应的完整路径,它是携带参数的)。

所以正确写法应该是:

 <!-- 笔记:使用$route.fullPath使完整路径与menu-item的index属性相匹配。
该组件只有default-active的值与index相同时才能高亮,
因此在路径携带query参数的情况下,使用完整路径可以使路径+参数进行对比,保证选中项可以高亮。
注意因为是完整路径,前面要加上/符号。-->

<el-menu :default-active="$route.fullPath" class="el-menu-demo" mode="horizontal" :router="true">
   <el-menu-item index="/Home">Home</el-menu-item>
      <el-submenu index="/ProductList" ref="Product">
         <template slot="title">Product</template>
            <el-menu-item v-for="(item,index) in productionType" :key="index" :index="'/ProductList?id='+item.id" :route="{name:'ProductList',query: { id: item.id }, params: { adpic: item.adpic, imgPath: webpath,catname:item.catname,catjc:item.catjc}}">{{ item.catname }} {{'/ProductList?id='+item.id}}</el-menu-item>
      </el-submenu>
   <el-menu-item index="/ContactUs">Contact us</el-menu-item>
</el-menu>

如果无法解决问题,也欢迎评论区讨论

<el-menu ellipsis class="el-menu-popper-demo" mode="horizontal" style="max-width: 1200px" active-text-color="#ffffff" :default-active="activePath" router > <el-menu-item index="index">首页</el-menu-item> <el-menu-item index="2" id="product">产品及解决方案</el-menu-item> <el-sub-menu :popper-offset="0" index="walkInto" @click="handleMenuClick"> <template #title>走进兴龙</template> <el-menu-item index="/walkInto#profile">公司简介</el-menu-item> <el-menu-item index="/walkInto#process">发展历程</el-menu-item> <el-menu-item index="/walkInto#news">新闻资讯</el-menu-item> </el-sub-menu> <el-menu-item index="user">用户服务</el-menu-item> <el-sub-menu :popper-offset="0" index="0"> <template #title>招贤纳士</template> <el-menu-item index="campus">校园招聘</el-menu-item> <el-menu-item index="society">社会招聘</el-menu-item> </el-sub-menu> <el-menu-item index="contact">联系我们</el-menu-item> <el-menu-item index="medium">媒体中心</el-menu-item> </el-menu> <script setup> import { useRouter,useRoute } from 'vue-router' import { ref, watch } from 'vue'; const route = useRoute(); const activePath = ref("index"); const updateBreadcrumbItems = (route) => { activePath.value = route.name; }; // 监听路由变化 watch( () => route.path, () => { updateBreadcrumbItems(route); } ); const router = useRouter() function goIndex() { router.push({name: 'index'}); } const handleMenuClick = () => { router.push('/walkInto'); }; </script> 这段代码点击el-sub-menu虽然跳转到了walkInto页面但是走进兴龙没有进行高亮显示 其中子菜单的 index="/walkInto#profile" 为锚点设置点击跳转到walkInto页面id为profile处的位置这个要进行更改 要如何点击el-sub-menu跳转到walkInto页面同时走进兴龙也进行高亮显示
最新发布
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值