Files
FlightScore-Frontend/flightscore/src/router/router.ts
T
CodingPhoenixx 4678963a22 first commit
2026-02-12 13:42:32 +01:00

29 lines
675 B
TypeScript

export type Route = {
path: string;
view: () => HTMLElement;
};
export class Router {
private routes: Route[];
private outlet: HTMLElement;
constructor(outlet: HTMLElement, routes: Route[]) {
this.routes = routes;
this.outlet = outlet;
window.addEventListener('popstate', () => this.resolve());
}
navigate(path: string) {
window.history.pushState({}, '', path);
this.resolve();
}
resolve() {
const path = window.location.pathname;
const match = this.routes.find((r) => r.path === path);
this.outlet.innerHTML = '';
this.outlet.append(
match ? match.view() : document.createElement('not-found-page')
);
}
}