let Vue;
class Router{
constructor(options) {
this.routeArr = options;
let current = window.location.hash.slice(1) || "/"
Vue.util.defineReactive(this,'routes', {path:current})
window.addEventListener('hashchange',()=>{
this.routes.path = window.location.hash.slice(1) || "/"
})
}
push(path){
window.location.href=`#${path}`;
}
}
const install = (_Vue)=>{
Vue = _Vue
Vue.mixin({
beforeCreate(){
if(this.$options.router){
this.$router = this.$options.router;
this.$routes = this.$options.router.routes;
}else{
this.$router = this.$parent && this.$parent.$router;
this.$routes = this.$parent && this.$parent.$router.routes
}
}
});
Vue.component('router-view',{
render(h){
let path = this.$routes.path;
let component = this.$router.routeArr.routes.filter((value)=>value.path === path)[0].component || null;
return h(component)
},
})
Vue.component('router-link', {
props:{
to:{
type: String,
required: true
}
},
render(h){
return h(
'a',
{
attrs:{
href:`#${this.to}`
}
},
this.$slots.default
)
},
})
}
export default {
Router,
install
}