vue源码探索之vue-router

110 阅读1分钟

基础介绍

路由模式

  • hash模式:页面url类似#/index,这种模式不会像服务器发送请求
  • history模式:页面url类似/index,这种模式刷新时会向服务器发送请求,需要服务端配置来支持这种路由模式 hash模式,是基于锚点和onhashchange事件来实现的;history模式是基于html5HistoryApipushStatereplaceState等来实现的,而pushStateIE10及以后才支持的。如果产品需要支持IE10以前的版本,只能采用hash模式

实现history模式路由

  • install:是一个静态方法,做了3件事情。判断当前插件是否已经安装过了,如果安装过直接返回;把Vue的构造函数注册在全局;把创建Vue的实例传入的router对象注入到Vue实例

  • 将router注入到Vue实例的时候需要注意因为beforeCreate方法在组件里面也会调用,但是我们只希望将router注入到Vue实例,所以需要判断当前是否是Vue实例

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;
            }
        }
    });
    // _Vue.prototype.$router = this.$options.router
}
  • constructor:将传入的参数储存一下;定义routerMap,用来存储路由和对应组件的映射关系;定义响应式对象data,包含一个current,用来代表当前路由
constructor(options) {
    this.options = options;
    this.routerMap = {};
    this.data = _Vue.observable({
        current: '/'
    })
    this.init()
}
  • createRouterMap:创建路由和对应组件的映射关系,保存到routerMap变量里面
createRouteMap() {
    //遍历所有的路由规则 吧路由规则解析成键值对的形式存储到routeMap中
    this.options.routes.forEach(route => {
        this.routerMap[route.path] = route.component;
    })
}
  • initComponent:注册router-linkrouter-view组件

1、router-link:返回一个a标签,并传入to属性,href属性指向to,添加点击方法,注意需要组织默认的方法,点击的时候调用history.pushState传入to,并将to赋给data.current

2、router-view:渲染组件,根据currentrouterMap中匹配对应的组件,并渲染到页面

initComponent(Vue) {
    Vue.component('router-link', {
        props: {
            to: String
        },
        render(h) {
            return h('a', {
                attrs: {
                    href: this.to
                },
                on: {
                    click: this.clickHandler
                },
            }, [this.$slots.default])
        },
        methods: {
            clickHandler(e) {
                e.preventDefault();
                history.pushState({}, '', this.to)
                this.$router.data.current = this.to
            }
        }
    })
    const self = this;
    Vue.component('router-view', {
        render(h) {
            const cm = self.routerMap[self.data.current]
            return h(cm)
        }
    })
}
  • initEvent:添加事件监听,监听popstate的变化,将window.location.pathname赋值给this.data.current
initEvent() {
    window.addEventListener('popstate', () => {
        this.data.current = window.location.pathname;
    })
}

最后附上完整的VueRouter类

/* eslint-disable */
let _Vue = null
export default class VueRouter {
    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;
                }
            }
        });
        // _Vue.prototype.$router = this.$options.router
    }
    constructor(options) {
        this.options = options;
        this.routerMap = {};
        this.data = _Vue.observable({
            current: '/'
        })
        this.init()
    }
    init() {
        this.createRouteMap()
        this.initComponent(_Vue)
        this.initEvent()
    }
    createRouteMap() {
        //遍历所有的路由规则 吧路由规则解析成键值对的形式存储到routeMap中
        this.options.routes.forEach(route => {
            this.routerMap[route.path] = route.component;
        })
    }
    initComponent(Vue) {
        Vue.component('router-link', {
            props: {
                to: String
            },
            render(h) {
                return h('a', {
                    attrs: {
                        href: this.to
                    },
                    on: {
                        click: this.clickHandler
                    },
                }, [this.$slots.default])
            },
            methods: {
                clickHandler(e) {
                    e.preventDefault();
                    history.pushState({}, '', this.to)
                    this.$router.data.current = this.to
                }
            }
        })
        const self = this;
        Vue.component('router-view', {
            render(h) {
                const cm = self.routerMap[self.data.current]
                return h(cm)
            }
        })
    }
    initEvent() {
        window.addEventListener('popstate', () => {
            this.data.current = window.location.pathname;
        })
    }
}