import Vue from 'vue'
import VueRouter from './testvue-router'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: () => import( '../views/About.vue')
}
]
const router = new VueRouter({
routes
})
export default router
let _Vue
class VueRouter {
constructor(options) {
this.$options = options
this.routeMap = {}
this.$options.routes.forEach(
route => {
this.routeMap[route.path] = route
})
const initial = window.location.hash.slice(1) || '/'
_Vue.util.defineReactive(this, 'current', initial)
window.addEventListener('hashchange', this.onHashChange.bind(this))
}
onHashChange() {
this.current = window.location.hash.slice(1)
console.log(this.current);
}
}
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,
require: true
},
},
render(h) {
return h('a', {
attrs: {
href: '#' + this.to
}
}, this.$slots.default)
}
})
Vue.component('router-view', {
render(h) {
const {routeMap, current} = this.$router
const component = routeMap[current] ? routeMap[current].component : null
return h(component)
}
})
}
export default VueRouter