Vue-- 实现简单版 vue-router

本文介绍了如何在Vue应用中实现简单版本的VueRouter3,主要涉及监听hash值变化、响应式数据处理以及如何在组件间传递当前路由信息。通过Vue插件机制,全局注册`router-link`和`router-view`组件,确保路由状态的变化能自动更新视图。
摘要由CSDN通过智能技术生成

实现简单版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;

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-router实现原理是利用浏览器提供的接口window.history和window.location.hash来实现路由功能。具体来说,vue-router通过配置mode属性来选择使用哪个接口实现路由功能。mode属性有两个选项:hash和history。当mode为hash时,vue-router使用window.location.hash来监听URL的变化,并根据URL的hash值来匹配对应的组件。当mode为history时,vue-router使用HTML5的history API来监听URL的变化,并根据URL的路径来匹配对应的组件。通过这种方式,vue-router能够在不刷新页面的情况下更新视图,实现前端路由的功能。\[1\]\[2\] 另外,vue-router还支持懒加载的实现方式。最常用的懒加载方式是通过import()来实现。通过在路由配置中使用import()来动态加载组件,可以实现按需加载,提高应用的性能。例如,可以将组件的import语句放在路由配置中的component属性中,当路由被访问时,才会动态加载对应的组件。这种方式可以减少初始加载的资源量,提高应用的加载速度。\[3\] #### 引用[.reference_title] - *1* [vue-router实现原理](https://blog.csdn.net/mengweiping/article/details/101068638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [超详细的vue-router原理](https://blog.csdn.net/jiangjialuan2/article/details/124799307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值