美文网首页
vue-Router原理实现

vue-Router原理实现

作者: lowpoint | 来源:发表于2021-02-25 23:55 被阅读0次

vue-Router有两种模式 Hash 与 History

  • Hash 模式是基于锚点,以及 onhashchange 事件
  • HIstory模式是基于 HTML5 中 History API
  • history.pushState() IE10以后支持 (history.push()会向服务器发送请求 history.pushState()记录地址,改变并记录地址 不会向服务器发送请求)
  • history.replaceState()

History 模式的使用

  • History需要服务器的支持
  • 单页应用中,服务不存在就会返回找不到页面
  • 在服务端应该除了静态资源外都返回单页应用的 index.html
server {
  listen  80;
  server_name localhost;

  location / {
    root html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html; //nginx 配置history模式
  }
}

VueRouter实现原理

Hash模式

  • URL中#后面的内容作为路径地址
  • 监听 hashchange事件
  • 根据当前路由地址找到对应组件重新渲染
  • 调用push方法先判断浏览器是否支持window.history.pushState方法 支持则直接调用 不支持则通过window.location改变地址栏
    History模式
  • 通过 history.pushState() 方法改变地址栏
  • 监听popstate 事件,可以监听到浏览器地址操作变化,记录改变后的地址
  • 根据当前路由地址找到对应组件重新渲染
  • window.history.pushState 不会触发 popstate事件,当历史状态被激活的时候 才会触发 popstate事件
//实现VueRouter
let _Vue = null
export default class VueRouter {

  constructor(options) {
    this.options = options
    this.routeMap = {} //键值对存储 路由地址:对应组件
    this.data = _Vue.observable({
      current: '/' //存储当前路由地址
    })
  }

  static install(Vue) {
    //1.判断插件是都已经安装
    if (VueRouter.install.installed) return
    VueRouter.install.installed = true
    //2.Vue构造函数记录在全局变量中
    _vue = Vue
    //3.把创建Vue实例时传入的router对象注册到Vue实例中
    //混入
    _Vue.mixin({
      beforeCreate() {
        if (this.$options.router) {
          _Vue.prototype.$router = this.$options.router
          this.$options.routerinit()
        }
      }
    })
  }

  init() {
    this.createRouteMap()
    this.initComponents(_Vue)
    this.initEvent()
  }

  //解析路由规则 存储到routeMap中
  createRouteMap() {
    this.options.routes.forEach(route => {
      this.routeMap[route.path] = route.component
    })
  }

  initComponents(Vue) {
    Vue.component('router-link', {
      props: {
        to: String
      },
      // template: '<a :href="to"><slot></slot></a>'
      render(h) { //h创建一个虚拟dom
        //三个参数 1.选择器 2.设置属性 3.生成元素的子元素
        return h('a', {
          attrs: {//设置dom对象属性
            href: this.to
          },
          on: {
            click: this.clickHandler
          }
        }, [this.$slots.default])
      },
      methods: {
        clickHandler(e) {
          //三个参数 1.data 2.网页标题 3.地址
          history.pushState({}, '', this.to)
          this.$router.data.current = this.to
          e.preventDefault()
        }
      }
    })

    const self = this

    Vue.component('router-view', {
      render(h) {
        const component = self.routeMap[self.data.current]
        return h(component)//将组件处理为虚拟dom
      }
    })
  }

  //注册popstate事件
  initEvent() {
    window.addEventListener('popstate', () => {
      this.data.current = window.location.pathname
    })
  }

}

Vue的构建版本

  • 运行版本:不支持template模板,需要打包的时候提前编译

  • 完整版:包含运行时和编译器,体积比运行版大10kb左右,程序运行的时候把模板转换成 render 函数

//vue.config.js
module.exports = {
  runtimeCompiler:true
}

相关文章

  • 手写 Vue Router、手写响应式实现、虚拟 DOM 和 D

    Vue-Router 原理实现 一、Vue-Router 动态路由 二、Vue-Router 嵌套路由 三、Vue...

  • vue总结

    vue路由守卫 vue实现双向绑定原理 Vue生命周期: vue跨域方式 vue-router实现原理 vue-r...

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

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

  • 第四天

    1、vue-router实现原理? 1.hash url后面会有#号 通过window.onhashchange...

  • Vue路由 ---- vue-router

    vue-router 中常用的 hash 和 history 路由模式实现原理吗? Two Point!!! sp...

  • 58转转前端面经

    一面: 自我介绍 vue双向绑定原理,用了js哪些方法实现 vue-router原理 ES6了解哪些 webpac...

  • vue-router实现原理

    1.编写路由,routes;包括路径,及映射组件 2.创建路由实例 mode设置为history表示利用了hist...

  • vue-router实现原理

    随着前端应用的业务功能起来越复杂,用户对于使用体验的要求越来越高,单面(SPA)成为前端应用的主流形式。大型单页应...

  • vue-Router原理实现

    vue-Router有两种模式 Hash 与 History Hash 模式是基于锚点,以及 onhashchan...

  • Vue-Router 原理实现

    使用步骤 1.创建router对象,router/index.js 2.注册router 对象, main.js ...

网友评论

      本文标题:vue-Router原理实现

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