Vue-- 实现简单版 vue-router

实现简单版vue-router3

前置知识:

      1、vue 插件机制 :

               vue.use(arg)  arg可以是一个函数和对象,需要有一个install方法,如果是函数(没有install方法),则直接执行该函数。install 方法第一个参数是 Vue 构造函数

思路(以hash为例)

        1、监听hash值改变,触发 router-view 更新

        2、如何在每个router-view 中拿到 当前地址和组件的对应关系,以及如何在hash值改变时自          动更新(本文思路是将 routeOptions 挂载在Vue实例上)     

注:

     Vue.util.defineReactive(this, 'current', init) 

    此方法是为了实现current响应式,只有current是响应式数据, router-view 里面的 render 函数才会随着数据变化而变化(当路由变化的时候this.current 改变) 

let Vue
class VueRouter {
  constructor(options) {
    console.log(options)
    let init = '/'
    // Vue.util.defineReactive(this, 'current', window.location.hash.slice(1))
    Vue.util.defineReactive(this, 'current', init)  // 实现响应式
    this.current = '/'  // 当前路径
    this.routes = options.routes || []
    this.mode = options.mode || 'hash' // 路由模式 hash 或者 history
    this.init()
  }
  init() {
    if (this.mode === 'hash') {
      location.hash = '/'
      // 项目第一次加载
      window.addEventListener('load', () => {
        this.current = location.hash.slice(1)
      })
      window.addEventListener('hashchange', () => {
        this.current = location.hash.slice(1)
      })
    }
  }

}

// install 居然在 constructor 先执行
VueRouter.install = function (_Vue) {
  console.log(_Vue)
  Vue = _Vue
  // 给调用组件添加一个属性router
  Vue.mixin({  // 每一个组件都会执行 beforeCreate 方法
    beforeCreate() {
      if (this.$options.router) { // 只有根实例才有 router 属性
        console.log('jbjbjb----->')
        Vue.prototype.$router = this.$options.router
      }
    }
  })
  // 全局组件
  // 创建全局组件 router-link 
  Vue.component('router-link', {
    props: {
      to: { // router-link 的to 属性
        type: String,
      }
    },
    render(h) {
      console.log('router-link---->', this.to)
      return h('a', { attrs: { href: '#' + this.to } }, this.$slots.default)
    }
  })
  // 创建全局组件 router-view 
  Vue.component('router-view', {
    render(h) { // 只有响应式数据变化才会 出发 render 方法
      console.log('router-view---->', this.$router)
      const current = this.$router.current
      const routes = this.$router.routes
      let com = routes.find((item) => {
        return item.path === current
      })
      return h(com.component)
    }
  })
}



export default VueRouter;

相关推荐

  1. Vue-- 实现简单 vue-router

    2024-03-18 08:42:03       42 阅读
  2. Vue-router

    2024-03-18 08:42:03       35 阅读
  3. vue-router

    2024-03-18 08:42:03       34 阅读
  4. Vue Router

    2024-03-18 08:42:03       30 阅读
  5. vue router

    2024-03-18 08:42:03       25 阅读
  6. 自定义插件vue-router简单实现hashRouter设计思路

    2024-03-18 08:42:03       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 08:42:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 08:42:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 08:42:03       87 阅读
  4. Python语言-面向对象

    2024-03-18 08:42:03       96 阅读

热门阅读

  1. C语言中大小写字母是如何转化的?

    2024-03-18 08:42:03       48 阅读
  2. Euler angles and Quaterean

    2024-03-18 08:42:03       40 阅读
  3. Leetcode 第388场周赛 问题和解法

    2024-03-18 08:42:03       42 阅读
  4. Redis 的数据类型及使用场景

    2024-03-18 08:42:03       38 阅读
  5. PyTorch学习笔记之激活函数篇(六)

    2024-03-18 08:42:03       37 阅读
  6. redis常见面试题

    2024-03-18 08:42:03       40 阅读
  7. Bean的实例化方式

    2024-03-18 08:42:03       39 阅读
  8. 在类Unix平台实现TCP客户端

    2024-03-18 08:42:03       33 阅读
  9. mysql提权总结(自学)

    2024-03-18 08:42:03       40 阅读
  10. 基于深度学习的车辆检测技术

    2024-03-18 08:42:03       37 阅读
  11. 程序分享--排序算法--桶排序

    2024-03-18 08:42:03       48 阅读