| 12
 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
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 
 | myrouter.jsimport HashHistory from "./hashHistory"
 class VueRouter {
 constructor(options) {
 
 this.options = options
 
 // 如果不传mode,默认为hash
 this.mode = options.mode || 'hash'
 
 // 判断模式是哪种
 switch (this.mode) {
 case 'hash':
 this.history = new HashHistory(this)
 break
 case 'history':
 // this.history = new HTML5History(this, options.base)
 break
 case 'abstract':
 
 }
 }
 init(app) {
 this.history.listen((route) => app._route = route)
 
 // 初始化时执行一次,保证刷新能渲染
 this.history.transitionTo(window.location.hash.slice(1))
 }
 
 // 根据hash变化获取对应的所有组件
 createMathcer(location) {
 const { pathMap } = createRouteMap(this.options.routes)
 
 const record = pathMap[location]
 const local = {
 path: location
 }
 if (record) {
 return createRoute(record, local)
 }
 return createRoute(null, local)
 }
 }
 
 let _Vue
 VueRouter.install = (Vue) => {
 _Vue = Vue
 // 使用Vue.mixin混入每一个组件
 Vue.mixin({
 // 在每一个组件的beforeCreate生命周期去执行
 beforeCreate() {
 if (this.$options.router) { // 如果是根组件
 // this 是 根组件本身
 this._routerRoot = this
 
 // this.$options.router就是挂在根组件上的VueRouter实例
 this.$router = this.$options.router
 
 // 执行VueRouter实例上的init方法,初始化
 this.$router.init(this)
 
 // 相当于存在_routerRoot上,并且调用Vue的defineReactive方法进行响应式处理
 Vue.util.defineReactive(this, '_route', this.$router.history.current)
 } else {
 // 非根组件,也要把父组件的_routerRoot保存到自身身上
 this._routerRoot = this.$parent && this.$parent._routerRoot
 // 子组件也要挂上$router
 this.$router = this._routerRoot.$router
 }
 }
 })
 Object.defineProperty(Vue.prototype, '$route', {
 get() {
 return this._routerRoot._route
 }
 })
 }
 
 function createRouteMap(routes) {
 
 const pathList = []
 const pathMap = {}
 
 // 对传进来的routes数组进行遍历处理
 routes.forEach(route => {
 addRouteRecord(route, pathList, pathMap)
 })
 
 console.log(pathList)
 // ["/home", "/home/child1", "/home/child2", "/hello", "/hello/child1"]
 console.log(pathMap)
 // {
 //     /hello: {path: xxx, component: xxx, parent: xxx },
 //     /hello/child1: {path: xxx, component: xxx, parent: xxx },
 //     /hello/child2: {path: xxx, component: xxx, parent: xxx },
 //     /home: {path: xxx, component: xxx, parent: xxx },
 //     /home/child1: {path: xxx, component: xxx, parent: xxx }
 // }
 
 
 // 将pathList与pathMap返回
 return {
 pathList,
 pathMap
 }
 }
 
 function addRouteRecord(route, pathList, pathMap, parent) {
 // 拼接path
 const path = parent ? `${parent.path}/${route.path}` : route.path
 const { component, children = null } = route
 const record = {
 path,
 component,
 parent
 }
 if (!pathMap[path]) {
 pathList.push(path)
 pathMap[path] = record
 }
 if (children) {
 // 如果有children,则递归执行addRouteRecord
 children.forEach(child => addRouteRecord(child, pathList, pathMap, record))
 }
 }
 
 function createRoute(record, location) {
 const res = []
 if (record) {
 while (record) {
 res.unshift(record)
 record = record.parent
 }
 }
 return {
 ...location,
 matched: res
 }
 }
 export default VueRouter
 
 hashHistory.js
 class HashHistory {
 constructor(router) {
 
 // 将传进来的VueRouter实例保存
 this.router = router
 
 // 一开始给current赋值初始值
 this.current = createRoute(null, {
 path: '/'
 })
 
 // 如果url没有 # ,自动填充 /#/
 ensureSlash()
 
 // 监听hash变化
 this.setupHashLister()
 }
 // 监听hash的变化
 setupHashLister() {
 window.addEventListener('hashchange', () => {
 // 传入当前url的hash
 this.transitionTo(window.location.hash.slice(1))
 })
 }
 
 // 跳转路由时触发的函数
 transitionTo(location) {
 console.log(location)
 
 // 找出所有对应组件
 let route = this.router.createMathcer(location)
 
 console.log(route)
 
 // hash更新时给current赋真实值
 this.current = route
 // 同时更新_route
 this.cb && this.cb(route)
 }
 // 监听回调
 listen(cb) {
 this.cb = cb
 }
 }
 
 // 如果浏览器url上没有#,则自动补充/#/
 function ensureSlash() {
 if (window.location.hash) {
 return
 }
 window.location.hash = '/'
 }
 
 export function createRoute(record, location) {
 const res = []
 if (record) {
 while (record) {
 res.unshift(record)
 record = record.parent
 }
 }
 return {
 ...location,
 matched: res
 }
 }
 
 export default HashHistory
 
 router-view组件
 const myView = {
 functional: true,
 render(h, { parent, data }) {
 const { matched } = parent.$route
 
 data.routerView = true // 标识此组件为router-view
 let depth = 0 // 深度索引
 
 while(parent) {
 // 如果有父组件且父组件为router-view 说明索引需要加1
 if (parent.$vnode && parent.$vnode.data.routerView) {
 depth++
 }
 parent = parent.$parent
 }
 const record = matched[depth]
 
 if (!record) {
 return h()
 }
 
 const component = record.component
 
 // 使用render的h函数进行渲染组件
 return h(component, data)
 
 }
 }
 export default myView
 
 router-link组件
 const myLink = {
 props: {
 to: {
 type: String,
 required: true,
 },
 },
 // 渲染
 render(h) {
 
 // 使用render的h函数渲染
 return h(
 // 标签名
 'a',
 // 标签属性
 {
 domProps: {
 href: '#' + this.to,
 },
 },
 // 插槽内容
 [this.$slots.default]
 )
 },
 }
 
 export default myLink
 
 //学习自掘金林三心老师的代码
 
 |