类图
根据VueRouter的使用,模拟实现一个VueRouter类。
类名:VueRouter |
属性: options:记录构造函数中传入的对象 data:响应式的对象,记录路由地址 routeMap:记录路由地址和组件间的关系 |
方法: _ install(Vue):void—-静态方法,实现vue.use() + Constructor(Options):VueRouter + init():void + initEvent():void—-注册popstate方法,监听浏览器历史 + initRouteMap():void—–初始化routerMap属性 + initComponents(Vue):void—–创建router-link及router-view组件 |
简单实现
简单模拟vue-router
实现基础路由跳转、router-view、router-link、历史记录操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| let _Vue = null
class VueRouter { static install(Vue) { if (VueRouter.install.installed) { return } VueRouter.install.installed = true
_Vue = Vue
_Vue.mixin({ beforeCreate() { if (this.$options.router) { _Vue.prototype.$router = this.$options.router this.$options.router.init() } } }) }
constructor(options) { this.options = options.routes; this.routerMap = {} this.data = _Vue.observable({ current:'/' }) }
init(){ this.initRouteMap() this.initCompinents(_Vue) this.initEvent() }
initRouteMap(){ this.options.forEach(item=>{ this.routerMap[item.path] = item.component }) }
initCompinents(Vue){ const _this = this; Vue.component('router-link',{ props:{ to:String }, render(h){ return h('a',{ attrs:{ href:this.to }, on:{ click:this.clickHandle } },[this.$slots.default]) }, methods:{ clickHandle(e){ history.pushState({},'',this.to) this.$router.data.current = this.to e.preventDefault() } } })
Vue.component('router-view',{ render(h){ const component = _this.routerMap[_this.data.current] return h(component) } }) }
initEvent(){ window.addEventListener('popstate',()=>{ this.data.current = window.location.pathname }) } }
export default VueRouter
|