面包屑实现

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

实现原理:

通过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();
    },
}
相关推荐
凉豆菌2 小时前
在html中如何创建vue自定义组件(以自定义文件上传组件为例,vue2+elementUI)
vue.js·elementui·html
广西千灵通网络科技有限公司2 小时前
基于 springboot+vue+elementui 的办公自动化系统设计(
vue.js·spring boot·elementui
可爱的秋秋啊15 小时前
vue3,element ui框架中为el-table表格实现自动滚动,并实现表头汇总数据
前端·vue.js·笔记·elementui
宝拉不想努力了20 小时前
vue element使用el-table时,切换tab,table表格列项发生错位问题
前端·vue.js·elementui
Dnn011 天前
修改el-select背景颜色
css·elementui·vue3
Aotman_2 天前
VUE Element-ui Message 消息提示组件自定义封装
前端·javascript·vue.js·ui·elementui·es6
serve the people3 天前
vue中将elementUI和echarts转成pdf文件
vue.js·elementui·echarts
tryCbest4 天前
Vue2集成ElementUI实现左侧菜单导航
前端·javascript·elementui
化作繁星5 天前
elementUI中MessageBox.confirm()默认不聚焦问题处理
前端·javascript·elementui
沉默是金~6 天前
Vue+Notification 自定义消息通知组件 支持数据分页 实时更新
javascript·vue.js·elementui