面包屑实现

背景:面包屑根据菜单内容显示不同内容。首页永远存在,后面的活动管理及多级菜单的面包屑展示。

实现原理:

通过this.$route.matched获取所有匹配路由,将处理首页外的其他路由设置到一个数组中,再通过数组循环方式显示

通过路由中的meta属性设置title用于显示面包屑;

监听路由,获取路由栈this.$route.matched

html 复制代码
    <el-breadcrumb separator="/">
      <el-breadcrumb-item v-for="(item,index) of levelList" :key="index">
      {{item.meta.title}}
      </el-breadcrumb-item>
    </el-breadcrumb>
javascript 复制代码
created() {
    this.getBreadcrumb();
  },
  watch: {
    $route(route) {
      if (route.path.startsWith("/redirect/")) {
        return;
      }
      this.getBreadcrumb();
    }
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(
        item => item.meta && item.meta.title
      );
      const first = matched[0];
      // 判断不是首页才concat
      if (!this.isDashboard(first)) {
        matched = [{ path: "/home", meta: { title: "首页" } }].concat(matched);
      }

      this.levelList = matched.filter(
        item => item.meta && item.meta.title && item.meta.breadcrumb !== false
      );
    },
    isDashboard(route) {
      const name = route && route.meta.title;
      if (!name) {
        return false;
      }
      return name.trim().toLocaleLowerCase() === "首页".toLocaleLowerCase();
    },
}
相关推荐
hackchen1 小时前
从0到1解锁Element-Plus组件二次封装El-Dialog动态调用
前端·vue.js·elementui
蓝胖子的多啦A梦3 小时前
Vue+element 日期时间组件选择器精确到分钟,禁止选秒的配置
前端·javascript·vue.js·elementui·时间选选择器·样式修改
夏天想3 小时前
vue2+elementui使用compressorjs压缩上传的图片
前端·javascript·elementui
海天胜景3 小时前
vue3 el-table 列增加 自定义排序逻辑
javascript·vue.js·elementui
满楼、10 天前
el-cascader 设置可以手动输入也可以下拉选择
javascript·vue.js·elementui
海天胜景10 天前
vue3 el-table 根据字段值 改变整行字体颜色
javascript·vue.js·elementui
资深柠檬精10 天前
报错 @import “~element-ui/packages/theme-chalk/src/index“;
前端·elementui
海天胜景10 天前
vue3 el-table 行颜色根据 字段改变
javascript·vue.js·elementui
我的心巴11 天前
Vue2 ElementUI Tree 拖动目标节点能否被放置及获取放置位置
前端·vue.js·elementui
刘 怼怼12 天前
Element UI 表格中实现搜索关键字高亮的
vue.js·ui·elementui