根据路由,动态高亮当前选择项

本文探讨了在前端开发中如何通过Vue路由的meta属性实现导航条目的自动高亮显示。通过对不同路径的匹配和meta.index的配置,可以有效地解决子路由激活状态的识别问题。

一开始判断path,发现子路由再输入url进入无法高亮

      判断当前的路径来设置活动标签activeNav
      changeActiveNav() {
        console.log(this.$route);
        switch (this.$route.path) {
          case '/w/kbap':
            this.activeNav = 1
            break;
          case '/w/zwgk':
            this.activeNav = 2
            break;
          case '/w/zxgg':
            this.activeNav = 3
            break;
          case '/w/fwrx':
            this.activeNav = 4
            break;
          case '/w/zxjy':
            this.activeNav = 5
            break;
          default:
            this.activeNav = 0;
        }
      }

使用路由的meta配置

        <ul class="navigation-ul">
          <li v-for="(item,index) in navList" class="navigation-li" :class="{activeNavigation:$route.meta.index==index||$route.matched[1].meta.index==index}"
            @click="toOtherNav(item,index)">{{item.title}}</li>
        </ul>
{
        path: 'kbap',
        component: kbapWeb,
        meta:{
          index:1
        }
      },
      {
        path: 'zwgk',
        component: zwgkWeb,
        meta:{
          index:2
        }
      },
      {
        path: 'zxgg',
        component: zxggWeb,
        meta:{
          index:3
        },

在Element Plus中,实现刷新页面后当前路由高亮有以下几种方法: ### 利用`default-active`属性绑定当前路由路径 `default-active`属性用于设置默认高亮的菜单,将其绑定为当前路由路径,这样即使刷新页面也不会失去高亮显示状态。比如当前页面是新闻维护,`default-active`的值就是`/Basic/News`。示例代码如下: ```vue <template> <el-menu :default-active="$route.path" mode="horizontal"> <el-menu-item index="/Basic/News">新闻维护</el-menu-item> <!-- 其他菜单 --> </el-menu> </template> ``` 上述代码通过将`default-active`绑定为`$route.path`,使当前路由对应的菜单高亮显示,刷新页面时也能保持高亮[^1]。 ### 针对多级子路由的处理 对于二级子路由和三级子路由以上的情况,可设置`:default-active="'/' + this.$route.path.split('/')[1] + '/' + this.$route.path.split('/')[2]"` ,以此解决刷新或前进后退时导航栏对应路由高亮错误的问题。示例代码如下: ```vue <template> <el-menu :default-active="getActiveIndex" mode="horizontal"> <!-- 菜单 --> </el-menu> </template> <script> export default { computed: { getActiveIndex() { const pathArr = this.$route.path.split('/'); return `/${pathArr[1]}/${pathArr[2]}`; } } } </script> ``` 此代码根据路由路径的分割结果,动态计算出`default-active`的值,确保多级子路由在刷新页面后能正确高亮[^1]。 ### vue3 + Element - plus动态路由配置实现高亮vue3中,可结合`useRoute`和`ref`来实现动态路由高亮。示例代码如下: ```vue <template> <el-menu :default-active="activeIndex" :router="true" :ellipsis="false" class="el-menu-demo" mode="horizontal" background-color='rgba(128,147,255)' text-color='#ffffff' active-text-color='rgba(128,147,255)'> <el-menu-item v-for="(i,item) in $route.matched[0].children" :key="item" :index = i.path>{{i.name}}</el-menu-item> </el-menu> </template> <script> import { useRoute } from 'vue-router'; import { ref } from 'vue'; export default { setup() { const activeIndex = ref(''); const route = useRoute(); const sesson = window.sessionStorage; sesson.Hearderpath = '/' + route.path.split('/')[1].toLowerCase(); activeIndex.value = sesson.Hearderpath; return { route, activeIndex }; } } </script> ``` 该代码通过`useRoute`获取当前路由信息,使用`ref`创建响应式数据`activeIndex`,并将其赋值为当前路由路径,实现动态路由高亮显示,刷新页面后依然有效[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值