二级导航栏加面包屑的实现

实现

<template>
  <div>
    <div class="twoNav">
      <!-- 二级导航菜单 -->
      <div class="left">
        <el-menu
          router
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          :active-text-color="activeTextColor"
          @select="handleSelect"
        >
          <el-menu-item
            v-for="el in routesArr"
            :key="el.name"
            :index="el.path"
          >
            {{ el.meta.title }}
          </el-menu-item>
        </el-menu>
      </div>
      <!-- 面包屑 -->
      <div class="rigth">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item
            v-for="item in breadList"
            :key="item.path"
            replace
            :to="item.path"
          >
            {{ item.meta.title }}
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "TwoNav",
  props: {
    activeTextColor: {
      type: String,
      default: "#3160A8",
    },
  },
  data() {
    return {
      // 面包屑
      breadList: [],
      // 选中
      activeIndex: "0",
      // 二级导航菜单
      routesArr: [],
    };
  },
  watch: {
    $route: {
      handler: "getPath",
      immediate: true,
    },
  },
  created() {
    this.initBreadList();
    const route = this.routes();
    // 二级导航菜单
    this.routesArr = route;
    this.activeIndex = this.$route.path;
  },
  methods: {
    getPath() {
      this.initBreadList();
      this.activeIndex = this.$route.path;
    },

    // 初始化面包屑
    initBreadList() {
      this.breadList = this.$route.matched;
    },
    // 二级导航选中
    handleSelect(key) {
      this.activeIndex = key;
    },
    // 路由
    routes() {
      var routes = {
        children: this.$router.options.routes,
      };

      var route = this.$route.matched;

      for (var i = 0; i < route.length - 1; i++) {
        routes = routes.children.find((e) => e.name == route[i].name);
      }

      return routes.children;
    },
  },
};
</script>
<style lang="scss" scoped>
.twoNav {
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 48px 0;
  box-sizing: border-box;
  border-bottom: 1px solid rgba(216, 216, 216, 1);
  .left {
    font-size: 24px;
  }
  .el-menu--horizontal > .el-menu-item {
    border-bottom: none;
    text-decoration: none;
  }
}
</style>

这里用到了两个重要的属性 :

  1. this.$route.matched (返回一个数组,包含当前路由的所有嵌套路径片段的路由记录) 如图:
    在这里插入图片描述

会返回一个平铺结构的数组,那么拿到数据我们就可以操作了 直接循环面包屑就可。

  1. this.$route.path (直接获取当前页面的url ) 如图:
    image.png
    为什么要获取url呢?因为我把key的值设置的是路由的path路径,点击导航栏的时候要调用组件内置事件handleSelect 给activeIndex赋值实现高亮,当然我这里只是其中一种思路,肯定会有更好的实现思路的。

遇到的问题

因为切换路由的同时面包屑也要同步更新,我最初的方案是使用watch来监听,这个想法没错,但是刚开始监听的是activeIndex,这就导致了一个问题,第一次点击切换导航栏的时候面包屑是不同步更新的。

解决

监听整个$route路由,这样就完美解决了上面的问题。记录一下过程,如有更好的思路,欢迎随时交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue 2中实现面包屑导航可以通过以下步骤完成: 1. 创建一个面包屑组件 `Breadcrumb.vue`: ```vue <template> <nav class="breadcrumb"> <ul> <li v-for="(crumb, index) in crumbs" :key="index" :class="{ active: index === crumbs.length - 1 }"> <router-link :to="crumb.path">{{ crumb.label }}</router-link> </li> </ul> </nav> </template> <script> export default { name: 'Breadcrumb', computed: { crumbs() { // 获取当前路由的路径 const routePath = this.$route.path; // 分割路径为数组 const pathSegments = routePath.split('/').filter(segment => segment !== ''); // 构建面包屑导航数据 const crumbs = pathSegments.map((segment, index) => { const path = `/${pathSegments.slice(0, index + 1).join('/')}`; return { path, label: segment }; }); return crumbs; } } }; </script> <style> .breadcrumb { background-color: #f5f5f5; padding: 10px; } .breadcrumb ul { list-style: none; padding: 0; margin: 0; } .breadcrumb li { display: inline-block; } .breadcrumb li:not(:last-child):after { content: '/'; margin: 0 5px; } .breadcrumb li.active { font-weight: bold; } </style> ``` 2. 在需要使用面包屑导航的组件中引入并使用 `Breadcrumb` 组件: ```vue <template> <div> <breadcrumb></breadcrumb> <!-- 其他组件内容 --> </div> </template> <script> import Breadcrumb from './Breadcrumb.vue'; export default { name: 'YourComponent', components: { Breadcrumb } }; </script> ``` 上述代码中,面包屑导航会根据当前路由的路径动态生成面包屑导航数据,并且只有最后一个路径会被标记为活动状态。你可以根据需要自定义样式和路由配置。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值