当前位置:首页 > VUE

自己实现vue-router

2026-01-20 08:15:54VUE

实现基本路由功能

创建一个简单的路由系统需要定义路由映射和监听URL变化。以下是一个基础实现:

class VueRouter {
  constructor(options) {
    this.routes = options.routes || []
    this.mode = options.mode || 'hash'
    this.current = window.location.hash.slice(1) || '/'
    this.routeMap = this.createMap(this.routes)

    window.addEventListener('load', () => {
      if (!window.location.hash) {
        window.location.hash = '/'
      }
    })

    window.addEventListener('hashchange', () => {
      this.current = window.location.hash.slice(1)
    })
  }

  createMap(routes) {
    return routes.reduce((map, route) => {
      map[route.path] = route.component
      return map
    }, {})
  }
}

实现路由视图组件

创建<router-view>组件用于显示匹配的组件内容:

Vue.component('router-view', {
  render(h) {
    const current = this.$router.current
    const routeComponent = this.$router.routeMap[current]
    return h(routeComponent)
  }
})

实现路由链接组件

创建<router-link>组件用于导航:

Vue.component('router-link', {
  props: {
    to: String
  },
  render(h) {
    return h('a', {
      attrs: {
        href: '#' + this.to
      }
    }, this.$slots.default)
  }
})

安装路由插件

将路由器实例注入Vue原型:

let _Vue
VueRouter.install = function(Vue) {
  _Vue = Vue

  Vue.mixin({
    beforeCreate() {
      if (this.$options.router) {
        Vue.prototype.$router = this.$options.router
      }
    }
  })
}

实现嵌套路由

支持嵌套路由需要递归匹配路径:

function createRouteMap(routes, pathMap = {}, parentPath = '') {
  routes.forEach(route => {
    const normalizedPath = parentPath + route.path
    pathMap[normalizedPath] = route.component

    if (route.children) {
      createRouteMap(route.children, pathMap, normalizedPath)
    }
  })
  return pathMap
}

实现动态路由

支持动态路径参数:

function createRouteMap(routes) {
  const pathList = []
  const pathMap = {}

  routes.forEach(route => {
    addRouteRecord(pathList, pathMap, route)
  })

  return {
    pathList,
    pathMap
  }
}

function addRouteRecord(pathList, pathMap, route, parent) {
  const path = parent ? `${parent.path}/${route.path}` : route.path
  const record = {
    path,
    component: route.component,
    parent
  }

  if (!pathMap[path]) {
    pathList.push(path)
    pathMap[path] = record
  }

  if (route.children) {
    route.children.forEach(child => {
      addRouteRecord(pathList, pathMap, child, record)
    })
  }
}

实现导航守卫

添加全局前置守卫功能:

class VueRouter {
  constructor(options) {
    this.beforeHooks = []
  }

  beforeEach(fn) {
    this.beforeHooks.push(fn)
  }

  navigateTo(path) {
    const hooks = this.beforeHooks
    const from = this.current
    const to = path

    runQueue(hooks, from, to, () => {
      this.current = to
      window.location.hash = to
    })
  }
}

function runQueue(queue, from, to, cb) {
  function next(index) {
    if (index >= queue.length) return cb()
    const hook = queue[index]
    hook(from, to, () => {
      next(index + 1)
    })
  }
  next(0)
}

实现HTML5历史模式

支持history API的路由模式:

class VueRouter {
  constructor(options) {
    this.mode = options.mode || 'hash'

    if (this.mode === 'history') {
      this.current = window.location.pathname
      window.addEventListener('popstate', () => {
        this.current = window.location.pathname
      })
    } else {
      // hash模式实现
    }
  }

  push(path) {
    if (this.mode === 'history') {
      window.history.pushState({}, '', path)
      this.current = path
    } else {
      window.location.hash = path
    }
  }
}

完整使用示例

将各部分组合后的使用方式:

自己实现vue-router

const router = new VueRouter({
  mode: 'hash',
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { 
      path: '/user', 
      component: User,
      children: [
        { path: 'profile', component: Profile }
      ]
    }
  ]
})

router.beforeEach((from, to, next) => {
  console.log(`Navigating from ${from} to ${to}`)
  next()
})

new Vue({
  router,
  el: '#app'
})

标签: vuerouter
分享给朋友:

相关文章

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除操…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…

简单实现vue github

简单实现vue github

实现一个简单的 Vue 项目并上传到 GitHub 创建 Vue 项目 使用 Vue CLI 快速初始化一个 Vue 项目,确保已安装 Node.js 和 npm/yarn。运行以下命令创建项目:…

vue实现转页面

vue实现转页面

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式完成,具体取决于项目结构和需求。 使用 router-link 组件 router-link 是 Vue Router…