zoukankan      html  css  js  c++  java
  • vueelementadmin面包屑导航分析

    面包屑导航

    1.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>

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

    2.路由与面包屑导航映射

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

    2.1.生成面包屑导航

    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)
    }
    • 获取 this.$route.matched,并过滤其中不包含 item.meta.title 的项,生成新的面包屑导航数组 matched
    • 判断 matched 第一项是否为 dashboard,如果不是,则添加 dashboard 为面包屑导航第一项
    • 再次过滤 matched 中 item.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()
    }

    2.2.渲染面包屑导航

    面包屑导航模板源码:

    <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>

    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))
    }

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

  • 相关阅读:
    ps图像渐变
    QPaintDevice: Cannot destroy paint device that is being painted
    QWidget::paintEngine: Should no longer be called
    权谋 — 朱元璋
    TL(简单)
    Access“输入的表达式中含有一个无效日期值”
    Qt label加边框
    Guardian of Decency(二分图)
    匈牙利算法的小总结
    Simple Molecules(简单)
  • 原文地址:https://www.cnblogs.com/pwindy/p/15566496.html
Copyright © 2011-2022 走看看