let Vue;
class VueRouter { constructor(options) { this.$options = options;
this.routeMap = {} this.$options.routes.forEach(route => { this.routeMap[route.path] = route })
Vue.util.defineReactive(this, 'current', '')
window.addEventListener('hashchange', this.onHashChange.bind(this)) window.addEventListener('load', this.onHashChange.bind(this)) }
onHashChange() { this.current = window.location.hash.slice(1) || '/' } }
VueRouter.install = function (_Vue) { Vue = _Vue
Vue.mixin({ beforeCreate() { if (this.$options.router) { Vue.prototype.$router = this.$options.router }
} })
Vue.component('router-link', { props: { to: { type: String, required: true }, }, render(h) { return h('a', { attrs: { href: '#' + this.to } }, this.$slots.default ) } }) Vue.component('router-view', { render(h) { let component = null const { routeMap, current } = this.$router if (routeMap[current]) { component = routeMap[current].component } return h(component) } }) }
export default VueRouter
|