fix
This commit is contained in:
33
resources/main/js/tg_app/SimpleRouter.js
Normal file
33
resources/main/js/tg_app/SimpleRouter.js
Normal file
@@ -0,0 +1,33 @@
|
||||
class SimpleRouter {
|
||||
constructor(routes) {
|
||||
this.routes = routes;
|
||||
this.init();
|
||||
}
|
||||
|
||||
init() {
|
||||
// window.addEventListener('popstate', () => this.route());
|
||||
// document.addEventListener('DOMContentLoaded', () => this.route());
|
||||
// document.addEventListener('click', (e) => {
|
||||
// if (e.target.tagName === 'A') {
|
||||
// e.preventDefault();
|
||||
// this.navigate(e.target.href);
|
||||
// }
|
||||
// });
|
||||
this.route();
|
||||
}
|
||||
|
||||
navigate(path) {
|
||||
window.history.pushState({}, '', path);
|
||||
this.route();
|
||||
}
|
||||
|
||||
route() {
|
||||
const path = window.location.pathname;
|
||||
const route = this.routes[path] || this.routes['/404'];
|
||||
if (route) {
|
||||
route();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {SimpleRouter}
|
||||
Reference in New Issue
Block a user