refactor(frontend): router refactoring

This commit is contained in:
syuilo 2025-03-19 18:06:22 +09:00
parent 2c76018b7f
commit 81ac71f7e5
10 changed files with 115 additions and 168 deletions

View file

@ -13,8 +13,8 @@ import { DI } from '@/di.js';
export type Router = Nirax<typeof ROUTE_DEF>;
export function createRouter(path: string): Router {
return new Nirax(ROUTE_DEF, path, !!$i, page(() => import('@/pages/not-found.vue')));
export function createRouter(fullPath: string): Router {
return new Nirax(ROUTE_DEF, fullPath, !!$i, page(() => import('@/pages/not-found.vue')));
}
export const mainRouter = createRouter(location.pathname + location.search + location.hash);
@ -24,23 +24,23 @@ window.addEventListener('popstate', (event) => {
});
mainRouter.addListener('push', ctx => {
window.history.pushState({ }, '', ctx.path);
window.history.pushState({ }, '', ctx.fullPath);
});
mainRouter.addListener('replace', ctx => {
window.history.replaceState({ }, '', ctx.path);
window.history.replaceState({ }, '', ctx.fullPath);
});
mainRouter.addListener('change', ctx => {
console.log('mainRouter: change', ctx.path);
console.log('mainRouter: change', ctx.fullPath);
analytics.page({
path: ctx.path,
title: ctx.path,
path: ctx.fullPath,
title: ctx.fullPath,
});
});
mainRouter.init();
export function useRouter(): Router {
return inject(DI.router, null) ?? mainRouter;
return inject(DI.router) ?? mainRouter;
}