面包屑实现

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

实现原理:

通过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();
    },
}
相关推荐
D_C_tyu9 天前
Vue3 + Element Plus | el-table 多级表头表格导出 Excel(含合并单元格、单元格居中)第二版
vue.js·elementui·excel
跟着珅聪学java9 天前
Element UI 的 el-input组件触发 blur事件
javascript·vue.js·elementui
Komorebi゛9 天前
【Vue + Element Plus】el-tree树结构样式改造,添加转折线
前端·javascript·vue.js·elementui
花生柿子9 天前
在elementui可横向滚动的table中,操作列有时候会透视下面的行
前端·javascript·elementui
敲代码的小吉米9 天前
Element Plus 表格中的复制功能使用指南
前端·javascript·elementui
小白探索世界欧耶!~16 天前
Vue2项目引入sortablejs实现表格行拖曳排序
前端·javascript·vue.js·经验分享·elementui·html·echarts
css趣多多18 天前
vue2项目改造为vue3遇到的问题以及解决办法
前端·vue.js·elementui
EstherNi19 天前
仿照elementui写图片放大的案例,但多加了下载按钮,vue3
javascript·vue.js·elementui
糕冷小美n21 天前
elementuivue2表格不覆盖整个表格添加固定属性
前端·javascript·elementui
沐墨染21 天前
黑词分析与可疑对话挖掘组件的设计与实现
前端·elementui·数据挖掘·数据分析·vue·visual studio code