面包屑导航

思考:你现在的项目中应用面包屑导航了吗?如果有,你是如何设计的?

el-breadcrumb-item

  • el-breadcrumb:面包屑导航容器,separator 控制面包屑导航文本中分割线
  • el-breadcrumb-item:面包屑子项目,可以使用 to 属性切换路由,slot 中可以包含 a 标签来跳转到外链
<el-breadcrumb separator="/">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item><a href="/">活动管理</a></el-breadcrumb-item>
  <el-breadcrumb-item>活动列表</el-breadcrumb-item>
  <el-breadcrumb-item>活动详情</el-breadcrumb-item>
</el-breadcrumb>
1
2
3
4
5
6

使用 to 属性和 a 标签切换路由区别是:to 属性切换路由是动态替换 App.vue 中的路由内容,而 a 标签切换路由会刷新页面

我们通过 Network 中查看请求可以论证这一点,使用 to 属性不会发送网络请求

路由与面包屑导航映射

面包屑导航最大的难度在于如何将路由与面包屑导航进行映射,下面我们一起看看 vue-element-admin 如何实现:

生成面包屑导航

getBreadcrumb() {
  let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
  const first = matched[0]

  if (!this.isDashboard(first)) {
    matched = [{ path: '/dashboard', meta: { title: 'Dashboard' }}].concat(matched)
  }

  this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
}
1
2
3
4
5
6
7
8
9
10

面包屑导航实现的逻辑如下:

  • 获取 this.$route.matched,并过滤其中不包含 item.meta.title 的项,生成新的面包屑导航数组 matched
  • 判断 matched 第一项是否为 dashboard,如果不是,则添加 dashboard 为面包屑导航第一项
  • 再次过滤 matcheditem.meta.title 为空的项和 item.meta.breadcrumb 为 false 的项

这里的关键是 this.$route.matched 属性,它是一个数组,记录了路由的匹配过程,这就是面包屑导航实现的基础

isDashboard 实现如下:

isDashboard(route) {
  const name = route && route.name
  if (!name) {
    return false
  }
  return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
}
1
2
3
4
5
6
7

渲染面包屑导航

面包屑导航模板源码:

<el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
        <span v-if="item.redirect==='noRedirect'||index==levelList.length-1" class="no-redirect">{{ item.meta.title }}</span>
        <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
</el-breadcrumb>
1
2
3
4
5
6
7
8

el-breadcrumb-item 内做了一个判断,如果是最后一个元素或者路由的 redirect 属性指定为 noRedirect 则不会生成链接,否则将使用 a 标签生成链接,但是这里使用了 @click.prevent 阻止了默认 a 标签事件触发,而使用自定义的 handleLink 方法处理路由跳转,handleLink 方法源码如下:

handleLink(item) {
  const { redirect, path } = item
  if (redirect) {
    this.$router.push(redirect)
    return
  }
  this.$router.push(this.pathCompile(path))
}
1
2
3
4
5
6
7
8

这里的 pathCompile 用于解决动态路由的匹配问题

上次更新: 11/17/2019, 11:38:59 AM