面包屑实现

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

实现原理:

通过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();
    },
}
相关推荐
fendouweiqian2 天前
element-plus 根据条件显示多选框
elementui
Jay丶萧邦3 天前
el-select:有关多选,options选项值不包含绑定值的回显问题
javascript·vue.js·elementui
我爱学习_zwj3 天前
后台管理系统-月卡管理
javascript·vue.js·elementui
是你的小熊啊3 天前
解决elementUi el-select 响应式不生效的问题
前端·vue.js·elementui
一个小开心呀3 天前
点击表格的最后一行的下拉框,会出现横向滚动条
前端·vue.js·elementui
Smile_Gently4 天前
前端:最简单封装nmp插件(组件)过程。
前端·javascript·vue.js·elementui·vue
BillKu4 天前
vue3中<el-table-column>状态的显示
javascript·vue.js·elementui
DCTANT5 天前
【原创】vue-element-admin-plus完成编辑页面中嵌套列表功能
前端·javascript·vue.js·elementui·typescript
CiL#5 天前
el-table已经选中的项,通过selectable属性不可以再次选择
javascript·vue.js·elementui
ew452185 天前
ElementUI表格表头自定义添加checkbox,点击选中样式不生效
前端·javascript·elementui