/
router.js
45 lines (40 loc) · 1.03 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Navigo from "navigo";
import { component } from "haunted";
export class Router {
constructor(root, routes) {
if (!Router.instance) {
Router.instance = this;
} else {
throw new Error("use getInstance");
}
this.router = new Navigo(root, false);
this.routes = routes;
return Router.instance;
}
static get getInstance() {
return Router.instance;
}
static inject(component) {
const outlet = document.querySelector("app-outlet");
while (outlet.firstChild) {
outlet.removeChild(outlet.firstChild);
}
outlet.appendChild(document.createElement(component));
}
init() {
this.routes.forEach(route => {
this.router
.on(route.path, params => {
if (!customElements.get(route.tag)) {
customElements.define(
route.tag,
component(route.page, HTMLElement, { useShadowDOM: false })
);
}
Router.inject(route.tag);
})
.resolve();
});
}
}
Router.instance = null;