面包屑实现

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

实现原理:

通过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();
    },
}
相关推荐
雪芽蓝域zzs21 小时前
第十节:动态侧边栏菜单(根据路由 meta 自动渲染菜单)
javascript·vue.js·elementui
m0_579146655 天前
Element UI 表格合并单元格完全指南:从原理到实战
前端·vue.js·elementui
常宇佳7 天前
vue3 实战系列之踩过的一些坑
javascript·vue.js·elementui
aXin_ya10 天前
ElementUI (01):Vue2 项目引入 ElementUI使用
elementui·vue
山川志~10 天前
Vue + Element UI 实战:如何实现表格时间、金额字段排序
vue.js·ui·elementui
Sterting18 天前
Form表单与校验
javascript·vue.js·elementui
Sterting19 天前
Element-Plus 入门与整体布局
前端·vue.js·elementui
yume_sibai20 天前
Element Plus 基础组件完全指南(12个核心组件)
前端·elementui
di24k24k1 个月前
多个 el-form 共用同一 ref 导致表单校验部分失效
前端·javascript·vue.js·elementui
森林的尽头是阳光1 个月前
tree回显问题
javascript·vue.js·elementui