继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

【学习打卡】第14天 Vue Element+Node.js开发企业通用管理后台系统(第8章)

离岛
关注TA
已关注
手记 15
粉丝 538
获赞 171

课程名称:Vue Element+Node.js开发企业通用管理后台系统(第8章)
课程章节: 第8章 登录功能开发(上)
主讲老师:Sam
课程内容:

今天学习的内容包括:

  • 面包屑导航

课程收获:

  1. 面包屑导航设计与应用
  • 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 标签切换路由会刷新页面。

路由与面包屑导航映射:

  • 生成面包屑导航,逻辑如下:

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

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)
}
  • 渲染面包屑导航
<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(item) {
  const { redirect, path } = item
  if (redirect) {
    this.$router.push(redirect)
    return
  }
  this.$router.push(this.pathCompile(path))
}

最后,附上课程截图 ending~

图片描述

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP