let Vue;
class Srouter {
constructor(options) {
this._options = options;
Vue.util.defineReactive(
this,
"current",
window.location.hash.slice(1) || "/"
);
window.addEventListener("hashchange", () => {
this.current = window.location.hash.slice(1);
});
}
}
Srouter.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,
},
},
created() {},
render(h) {
return h("a", { attrs: { href: `#${this.to}` } }, this.$slots.default);
},
});
Vue.component("router-view", {
render(h) {
let component = null;
let obj = this.$router._options.routes.find(
(item) => item.path === this.$router.current
);
if (obj) {
component = obj.component;
}
return h(component);
},
});
};
export default Srouter;