This commit is contained in:
Han
2025-02-12 10:09:30 +08:00
parent 8deb0fe01e
commit d116e8fcd1
912 changed files with 5761 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
type RouterEventKey = "beforeCreate" | "created" | "beforeMount" | "mounted" | "afterMount";
type RouterEventName = "astro:before-preparation" | "astro:after-preparation" | "astro:before-swap" | "astro:after-swap" | "astro:page-load";
type EventHandler = (event: Event) => void;
// 路由事件映射
const routerFn: Record<RouterEventKey, RouterEventName> = { beforeCreate: 'astro:before-preparation', created: 'astro:after-preparation', beforeMount: 'astro:before-swap', mounted: 'astro:after-swap', afterMount: 'astro:page-load' };
const updateRouter = (key: RouterEventKey, handler: EventHandler) => {
const eventName = routerFn[key];
if (!eventName) {
throw new Error(`Invalid key ${key} for router update. Valid keys are: ${Object.keys(routerFn).join(", ")}.`);
}
document.removeEventListener(eventName, handler);
document.addEventListener(eventName, handler);
};
export default updateRouter;