面包屑实现

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

实现原理:

通过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();
    },
}
相关推荐
肖老师xy3 天前
Ai生成时间排期进度
javascript·vue.js·elementui
Komorebi゛4 天前
【Vue3+Element Plus】el-dialog弹窗点击遮罩层无法关闭弹窗问题记录
前端·vue.js·elementui
J总裁的小芒果4 天前
后端返回参数不一致 前端手动处理key
前端·vue.js·elementui
一入程序无退路4 天前
vue中序号不能按排序显示
javascript·vue.js·elementui
牛先森家的牛奶5 天前
elementUI的table合并行和列模板
前端·javascript·elementui
Aotman_5 天前
Vue MutationObserver 监听
前端·javascript·vue.js·elementui·前端框架·ecmascript
前端小怪兽zmy6 天前
Vue3实现纯前端语音输入成文字显示
前端·javascript·elementui
Aotman_6 天前
Vue.directive:自定义指令及传参
前端·javascript·vue.js·elementui·ecmascript·es6
我这一生如履薄冰~9 天前
element-plus去除el-dropdown组件当鼠标移入文本时会出现边框
前端·elementui·vue
D_C_tyu10 天前
Vue3 + Element Plus | el-table 表格获取排序后的数据
javascript·vue.js·elementui