美文网首页
前端路由探究(2) vue-router源码学习-两大组件

前端路由探究(2) vue-router源码学习-两大组件

作者: kuulid | 来源:发表于2017-12-17 13:57 被阅读81次

使用vue-router时候,肯定会使用router-link和router-view两个组件,其中router-view是作为路由的替换区域,router-link是作为路由的导航区域。

router-view

这个组件的功能很简单,就是拿到当前匹配的组件,然后返回渲染出来就好。
打开工程文件夹/src/components/vies.js

//parent为挂载的父元素
// depth为当前组件的深度
 let depth = 0
 let inactive = false
 while (parent && parent._routerRoot !== parent) {
      if (parent.$vnode && parent.$vnode.data.routerView) {
        depth++
      }
      //处理kept-alive的逻辑
      if (parent._inactive) {
        inactive = true
      }
      parent = parent.$parent
 }
 data.routerViewDepth = depth

  // render previous view if the tree is inactive and kept-alive
  if (inactive) {
    return h(cache[name], data, children)
  }
  //取得route的匹配项
  const matched = route.matched[depth]
    // render empty node if no matched route
  if (!matched) {
      cache[name] = null
      return h()
  }

  const component = cache[name] = matched.components[name]

主要代码如上,英文是本来的注释,后面就是处理props的一些东西,最后返回

return h(component, data, children);

router-link

先看一下props:

  props: {
   //要转向的路径
    to: {
      type: toTypes,
      required: true
    },
//生成的html标签,默认为<a>
    tag: {
      type: String,
      default: 'a'
    },
    // 完整模式,如果为 true 那么也就意味着
    // 绝对相等的路由才会增加 activeClass
    // 否则是包含关系
    exact: Boolean,
   //添加的路径
    append: Boolean,
    replace: Boolean,
  //链接激活和跳转的css class名称
    activeClass: String,
    exactActiveClass: String,
  //绑定的事件
    event: {
      type: eventTypes,
      default: 'click'
    }
  }

如何绑定事件

    const handler = e => {
      if (guardEvent(e)) {
        if (this.replace) {
          router.replace(location)
        } else {
          router.push(location)
        }
      }
    }

    const on = { click: guardEvent }
    if (Array.isArray(this.event)) {
      this.event.forEach(e => { on[e] = handler })
    } else {
      on[this.event] = handler
    }

生成html标签,并且绑定事件

    if (this.tag === 'a') {
      data.on = on
      data.attrs = { href }
    } else {
      // find the first <a> child and apply listener and href
      const a = findAnchor(this.$slots.default)
      if (a) {
        // in case the <a> is a static node
        a.isStatic = false
        const extend = _Vue.util.extend
        const aData = a.data = extend({}, a.data)
        aData.on = on
        const aAttrs = a.data.attrs = extend({}, a.data.attrs)
        aAttrs.href = href
      } else {
        // doesn't have <a> child, apply listener to self
        data.on = on
      }
   }

相关文章

  • 前端路由探究(2) vue-router源码学习-两大组件

    使用vue-router时候,肯定会使用router-link和router-view两个组件,其中router-...

  • vue router

    简单的路由原理,基于组件 vue-router学习 1.使用 2.入口 3.JavaScript 动态路由监听$r...

  • 一起学Vue:路由(vue-router)

    前言 学习vue-router就要先了解路由是什么?前端路由的实现原理?vue-router如何使用?等等这些问题...

  • vue-router 源码实现前端路由的两种方式

    在学习 vue-router 的代码之前,先来简单了解一下前端路由。 前端路由主要有两种实现方法: Hash 路由...

  • DAY1

    配置路由流程 1、用命令安装路由组件 $ cnpm install vue-router --save 2、mai...

  • Vue 学习笔记 vue-router路由和前端状态管理

    Vue 学习笔记十一、vue-router路由和前端状态管理 什么是路由:网址 11.1 vue­-router路...

  • 与Vue.js的第九天

    今天学习了路由 路由 路由 路由vue-router是vue的核心组件根据不同的url访问不同页面配合单页面使用 ...

  • Vue 基础 - 前端路由

    使用vue-router实现前端路由 安装vue-router: 配置路由文件,并在vue实例中导入: 设置视图(...

  • vue路由

    今日目标 1.能够说出什么是路由2.能够说出前端路由的实现原理3.能够使用Vue-Router实现前端路由4.能够...

  • vue路由

    今日目标 1.能够说出什么是路由2.能够说出前端路由的实现原理3.能够使用Vue-Router实现前端路由4.能够...

网友评论

      本文标题:前端路由探究(2) vue-router源码学习-两大组件

      本文链接:https://www.haomeiwen.com/subject/vvtjwxtx.html