VueRouter实现原理

2021-11-28  本文已影响0人  hao102

VueRouter类图

image.png

VueRouter类

属性:

options:记录构造函数中传入的对象;路由规则
routeMap:是一个对象;用来记录路由地址和组件之间的对应关系;即将传入的路由规则解析到routeMap对象中
data:是一个响应式的对象;里边有一个current用来记录当前的路由地址
install方法:是一个静态方法;用来实现Vue的插件机制
Constructor:构造函数,帮我们初始化类属性
initEvent:用来注册popState事件;监听浏览器历史的变化
createRouteMap方法:将构造函数中传入的路由规则转换为键值对的形式存储到routeMap属性中
initComponents方法:用来创建routeLink和routeView组件的
init方法:调用initEvent,createRouteMap,initComponents方法

/*
 * @Descripttion: 封装一个简单的VueRouter
 * @version: 
 * @Author: ahao
 * @Date: 2021-11-27 19:38:39
 * @LastEditors: ahao
 * @LastEditTime: 2021-11-27 21:37:30
 */

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({
            beforeCreated() {
                // 只在vue实例上执行;组件上不执行
                if (this.$options.router) {
                    _Vue.prototype.$router = this.$options.router;
                    this.$options.router.init();
                }
            }
        })
        // _Vue.prototype.$router = this.$options.router;
    }

    constructor(options) {
        this.options = options;
        this.routeMap = {};
        this.data = _Vue.observable({
            // 创建一个响应式的对象
            current: '/'
        })
    }
    createRouteMap() {
        // 遍历所有的路由规则;把路由规则解析成键值对的形式存储到routeMap中
        this.options.routes.forEach(route => {
            this.routeMap[route.path] = route.component;
        })
    }
    initComponents(Vue) {
        const self = this;
        Vue.component('route-link', {
            props: {
                to: String
            },
            render(h) {
                return h('a', {
                    attrs: {
                        href: this.to;
                    },
                    on: {
                        click: this.clicHandler
                    }
                }, [this.$slots.default])
            },
            methods: {
                clicHandler(e) {
                    history.pushState({}, '', this.to);
                    this.$router.data.current = this.to;
                    e.preventDefault();
                }
            }

            // template: '<a :href="to"><slot></slot></a>'
        })
        Vue.component('route-view', {
            render(h) {
                const component = self.routeMap[self.data.current]

                return h(component)
            }
        })
    }
    initEvent() {
        // 
        window.addEventListener('popstate', () => {
            this.data.current = window.location.pathname
        })
    }

    init() {
        this.createRouteMap();
        this.initComponents(_Vue);
        this.initEvent();
    }
}
上一篇下一篇

猜你喜欢

热点阅读