面包屑实现

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

实现原理:

通过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();
    },
}
相关推荐
向前跑丶加油16 小时前
解决 Element Plus 中 Tooltip 样式影响全局菜单(Menu)及宽度控制失效的完美方案
css·elementui
渔舟小调2 天前
P18 | Element Plus 通用 CRUD 页面模板:一个模板覆盖 80% 管理页面
javascript·vue.js·elementui
BangD3 天前
前端elementUI el-form个别字段增加校验
前端·vue.js·elementui
一个打工仔的笔记6 天前
vue3 elementui plus 可编辑表格 完整例子
前端·vue.js·elementui
1314lay_10077 天前
el-table表格数据分页切片,导致表格的多选失效
javascript·vue.js·elementui
1314lay_10077 天前
Vue+C#根据配置文件实现动态构建查询条件和动态表格
javascript·vue.js·elementui·c#
1314lay_10077 天前
axios的Post方法和Delete方法的参数个数和位置不同,导致415错误
前端·javascript·vue.js·elementui
A_nanda8 天前
Vue2 表单提交异常详细排查方案
javascript·vue.js·elementui
千码君201612 天前
kotlin:jetpack compose 生成动态可控的动画
vue.js·elementui·kotlin
牧杉-惊蛰12 天前
修改表格选中时的背景色与鼠标滑过时的背景色
前端·javascript·css·vue.js·elementui·html