vue+element-ui 封装面包屑导航

面包屑导航在很多项目中都会大量的使用,所以我们最好将封装成组件用来调用。在刚开始封装的时候在网上也找了许多方法,但是发现大多的方法都比较臃肿,而且对于项目后期的维护造成了很大的工作量。最终还是自己封装了一个。

1.新建bread.vue文件;

<template>
 <div class="bread">
   <div class="example-container">
     当前位置:
    <el-breadcrumb separator=">">
      <el-breadcrumb-item
        v-for="(item,index) in breadList"
        :key="index"
        :to="{ path: item.path }"
      >{
    
    {
    
    item.meta.title}}</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
 </div>
</template>
<script>
export default {
    
    
  data () {
    
    
    return {
    
    
      breadList: [] // 路由集合
    }
  },
  watch:{
    
    
    $route(){
    
    
      this.getBreadcrumb();
    }
  },
  methods: {
    
    
    isHome(route) {
    
    
      return route.name === "首页";
    },
    getBreadcrumb() {
    
    
      let matched = this.$route.matched;
      if (!this.isHome(matched[0])) {
    
    
        matched = [{
    
     path: "/", meta: {
    
     title: "首页" } }].concat(matched);
      }
      this.breadList = matched;
    }
  },
   created() {
    
    
    this.getBreadcrumb();
  }
}
</script>
<style scoped>
.bread  /deep/ .el-breadcrumb{
    
    
  display: inline-block;
  display: inline-block;
  height: 48px;
  vertical-align: top;
  line-height: 48px;
}
.bread /deep/ .is-link{
    
    
  font-weight: normal;
}
.bread{
    
    
  float:left;
}
</style>

2.在需要调用页面引入该插件

<bread></bread>


import bread from "@/components/bread"
export default {
    
    
  name: 'index',
  components: {
    
    
     bread
  },

3.在路由中增加mate属性
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36229632/article/details/103399345