面包屑实现

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

实现原理:

通过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();
    },
}
相关推荐
zhangyao9403308 天前
开发pc端时,表格的高度怎么设置才能铺满页面
前端·javascript·elementui
mengqudoh10 天前
elementui el-table 表头固定功能
javascript·vue.js·elementui
陪小甜甜赏月11 天前
ElementPlus 多个并列 Table 独立全选/取消全选 (适配嵌套表格业务)
前端·vue.js·elementui
大可-12 天前
CSDN博客-星火知识库教程
前端·javascript·vue.js·elementui·html
Liu.77416 天前
Vue3结合Element Plus封装点击查看大图的自定义指令
javascript·vue.js·elementui
跟着珅聪学java17 天前
Element UI 的 Tabs 标签页开发教程
javascript·vue.js·elementui
不是山谷.:.18 天前
Axios的【接口防抖 + 请求失败重试 + 弱网提示】三合一高阶版封装
前端·javascript·vue.js·笔记·elementui·typescript
镜宇秋霖丶23 天前
2026.5.12@霖宇博客制作中遇见的问题
前端·vue.js·elementui
镜宇秋霖丶1 个月前
2026.5.10@霖宇博客制作中遇见的问题
前端·vue.js·elementui
莫生灬灬1 个月前
ElementUI封装 共91个组件 支持易语言/火山/C#/Python
开发语言·c++·python·ui·elementui·c#