结合element面包屑组件创建动态路由

$route.matched

1、一个数组,包含当前路由的所有嵌套路径片段的路由记录
2、 一个路由匹配到的所有路由记录会暴露为 $route 对象 (还有在导航守卫中的路由对象) 的 $route.matched 数组

<template>
  <div id="example-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item
        v-for="(item,index) in breadList"
        :key="index"
        @click.native="breadcrumbClick(item, index)"
      >{{item.title}}</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
</template>

<script>
export default {
  data () {
    return {
      breadList: [] // 路由集合
    }
  },
  watch: {},
  computed: {},
  mounted () {
    this.getBreadcrumb()
  },
  methods: {
    breadcrumbClick (item, index) {
      if (index === this.breadList.length - 1) {
        return
      }
      this.$router.push(item.path)
    },
    getBreadcrumb () {
      let matched = this.$route.matched
      let res = []
      for (const key in matched) {
        if (matched.hasOwnProperty(key)) {
          const element = matched[key]
          const title = element.meta.title
          // if (!title) {
          //   res = []
          //   break
          // }
          res.push({
            path: element.path,
            title
          })
        }
      }
      this.breadList = res
    }
  }
}
</script>

<style lang="scss">
@import '@/styles/index.scss';
#example-container{
  background: $primary-bg-color;
  .el-breadcrumb__inner {
    text-decoration: none;
    -webkit-transition: color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    transition: color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    color: $sedondary-text-color;
  }
  .el-breadcrumb__inner:hover {
    color: $sedondary-text-color;
    cursor: pointer;
  }
  .el-breadcrumb__item:last-child{
    .el-breadcrumb__inner{
      font-weight: normal;
    }
  }
}
</style>

效果图:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值