前言
这里只手动实现了
router-link和router-view
首先 - 创建一个类
我们知道
vue接受一个install方法用来注册,vue-router也不例外,不知道读者们有没有注意vue-router的注册并不是用的Vue.use()方法(有兴趣的读者可以去了解一下,这里就不过多介绍了)
// 这里用来保存传进来的 Vue 实例
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 () {
// 只有vue实例才有this.$options.router,组件中没有,也体现了没有用vue.user 方法注册,而是在 new Vue 的时候在 options 中传入
// 此时的 this.$options.router 指向的 VueRouter 这个类
// new Vue({
// router,
// render: h => h(App)
// }).$mount('#app')
if (this.$options.router) {
_Vue.prototype.$router = this.$options.router
// init() 方法在下边实现
this.$options.router.init()
}
}
})
}
创建需要用的变量
- options:传进来的路由配置
- routeMap:路由规则
- data:响应式的路径(Vue.observable是 vue 提供的一个响应式方法)
constructor (options) {
this.options = options
this.routeMap = {}
this.data = _Vue.observable({
current: '/'
})
}
init 方法
- 路由规则的处理
- 组件的实现 router-link、router-view
- 浏览发生变化,同步到路由内部
init () {
// 路由规则的处理
this.createRouteMap()
// 组件的实现 router-link、router-view
this.initComponents(_Vue)
// 浏览发生变化,同步到路由内部
this.initEvent()
}
init 方法中的具体实现
createRouteMap () {
// 遍历所有的路由规则,把路由规则解析成键值对的形式,存储到 routeMap 中
this.options.routes.forEach(route => {
this.routeMap[route.path] = route.component
})
}
initComponents (Vue) {
Vue.component('router-link', {
props: {
to: String
},
// 运行版vue,不包含编译模版,所以需要手动开启:runtimeCompiler: true
// template: `<a :href="to"><slot></slot></a>`
render (h) {
return h('a', {
attrs: {
href: this.to
},
on: {
click: this.clickHandler
}
}, [this.$slots.default])
},
methods: {
clickHandler (e) {
history.pushState({}, '', this.to)
// this.$router 指向的 VueRouter 这个类
this.$router.data.current = this.to
e.preventDefault()
}
}
})
const _this = this
Vue.component('router-view', {
render (h) {
const component = _this.routeMap[_this.data.current]
return h(component)
}
})
}
initEvent () {
// 浏览器地址栏变化,对应组件发生相应变化
window.addEventListener('popstate', () => {
this.data.current = window.location.pathname
})
}
}
完整代码
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
this.$options.router.init()
}
}
})
}
constructor (options) {
this.options = options
this.routeMap = {}
this.data = _Vue.observable({
current: '/'
})
}
init () {
this.createRouteMap()
this.initComponents(_Vue)
this.initEvent()
}
createRouteMap () {
// 遍历所有的路由规则,把路由规则解析成键值对的形式,存储到 routeMap 中
this.options.routes.forEach(route => {
this.routeMap[route.path] = route.component
})
}
initComponents (Vue) {
Vue.component('router-link', {
props: {
to: String
},
// 运行版vue,不包含编译模版,所以需要手动开启:runtimeCompiler: true
// template: `<a :href="to"><slot></slot></a>`
render (h) {
return h('a', {
attrs: {
href: this.to
},
on: {
click: this.clickHandler
}
}, [this.$slots.default])
},
methods: {
clickHandler (e) {
history.pushState({}, '', this.to)
// this.$router 指向的 VueRouter 这个类
this.$router.data.current = this.to
e.preventDefault()
}
}
})
const _this = this
Vue.component('router-view', {
render (h) {
const component = _this.routeMap[_this.data.current]
return h(component)
}
})
}
initEvent () {
// 浏览器地址栏变化,对应组件发生相应变化
window.addEventListener('popstate', () => {
this.data.current = window.location.pathname
})
}
}