|
7 | 7 | * Copyright (c) 2024 by wuyifan0203 email: 1208097313@qq.com, All Rights Reserved. |
8 | 8 | */ |
9 | 9 | import { useRouter } from 'vitepress'; |
| 10 | +import { watch, onMounted } from 'vue'; |
| 11 | + |
| 12 | +const beforeCallbackList: Function[] = []; |
| 13 | +let beforeHookInitialized = false; |
10 | 14 |
|
11 | | -const beforeCallbackMap: { [key: string]: Function } = {}; |
12 | 15 | const useBeforeRouterChange = (callback: Function) => { |
13 | | - if (Object.keys(beforeCallbackMap).length === 0) { |
| 16 | + beforeCallbackList.push(callback); |
| 17 | + |
| 18 | + if (!beforeHookInitialized) { |
| 19 | + beforeHookInitialized = true; |
14 | 20 | const router = useRouter(); |
15 | 21 | const cacheBeforeChange = router.onBeforeRouteChange; |
| 22 | + |
16 | 23 | router.onBeforeRouteChange = async (to: string) => { |
17 | | - for (const key in beforeCallbackMap) { |
18 | | - await beforeCallbackMap[key](to); |
| 24 | + // 执行所有注册的回调 |
| 25 | + for (const callback of beforeCallbackList) { |
| 26 | + try { |
| 27 | + await callback(to); |
| 28 | + } catch (error) { |
| 29 | + console.error('Error in beforeRouteChange callback:', error); |
| 30 | + } |
19 | 31 | } |
| 32 | + // 执行原始钩子 |
20 | 33 | cacheBeforeChange && cacheBeforeChange(to); |
21 | 34 | } |
22 | 35 | } |
23 | | - // 不用数组因为没法去重 |
24 | | - beforeCallbackMap[callback.toString()] = callback; |
25 | | - |
26 | | - console.log(beforeCallbackMap, 'callbackMap'); |
27 | | - |
28 | 36 | } |
29 | 37 |
|
30 | | -const afterCallbackMap: { [key: string]: Function } = {}; |
31 | | -const useAfterRouterChange = (callback: Function) => { |
32 | | - if (Object.keys(afterCallbackMap).length === 0) { |
33 | | - const router = useRouter(); |
34 | | - const cacheAfterChange = router.onAfterRouteChanged; |
35 | | - router.onAfterRouteChanged = async (to: string) => { |
36 | | - for (const key in afterCallbackMap) { |
37 | | - await afterCallbackMap[key](to); |
| 38 | +const afterCallbackList: Function[] = []; |
| 39 | +let afterWatchInitialized = false; |
| 40 | +let routerInstance: any = null; |
| 41 | + |
| 42 | +const initRouteWatcher = () => { |
| 43 | + if (afterWatchInitialized || !routerInstance) return; |
| 44 | + |
| 45 | + console.log('Initializing route watcher...'); |
| 46 | + afterWatchInitialized = true; |
| 47 | + |
| 48 | + // 使用 watch 监听 route.path 变化 |
| 49 | + watch( |
| 50 | + () => routerInstance.route.path, |
| 51 | + async (newPath, oldPath) => { |
| 52 | + // 跳过初始加载(oldPath 为空字符串) |
| 53 | + if (!oldPath) { |
| 54 | + console.log('Initial load, skip callback execution'); |
| 55 | + return; |
38 | 56 | } |
39 | | - cacheAfterChange && cacheAfterChange(to); |
| 57 | + |
| 58 | + console.log('=== Route changed ==='); |
| 59 | + console.log('From:', oldPath, 'To:', newPath); |
| 60 | + console.log('Callbacks count:', afterCallbackList.length); |
| 61 | + |
| 62 | + // 执行所有注册的回调 |
| 63 | + for (let i = 0; i < afterCallbackList.length; i++) { |
| 64 | + const cb = afterCallbackList[i]; |
| 65 | + try { |
| 66 | + console.log(`Executing callback [${i}]:`, cb.name || 'anonymous'); |
| 67 | + await cb(newPath); |
| 68 | + console.log(`Callback [${i}] completed`); |
| 69 | + } catch (error) { |
| 70 | + console.error(`Error in callback [${i}]:`, error); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + console.log('All callbacks executed'); |
40 | 75 | } |
| 76 | + ); |
| 77 | + |
| 78 | + console.log('Route watcher initialized successfully'); |
| 79 | +}; |
| 80 | + |
| 81 | +const useAfterRouterChange = (callback: Function) => { |
| 82 | + afterCallbackList.push(callback); |
| 83 | + console.log('Registered after route change callback, total:', afterCallbackList.length); |
| 84 | + |
| 85 | + // 获取 router 实例 |
| 86 | + if (!routerInstance) { |
| 87 | + routerInstance = useRouter(); |
| 88 | + } |
| 89 | + |
| 90 | + // 在 onMounted 中初始化 watch,确保在正确的 Vue 上下文中 |
| 91 | + if (typeof window !== 'undefined') { |
| 92 | + onMounted(() => { |
| 93 | + initRouteWatcher(); |
| 94 | + }); |
41 | 95 | } |
42 | | - // 不用数组因为没法去重 |
43 | | - afterCallbackMap[callback.toString()] = callback; |
44 | 96 | } |
45 | 97 |
|
46 | | - |
47 | | - |
48 | 98 | export { useBeforeRouterChange, useAfterRouterChange } |
49 | | - |
50 | | -// 另一种写法, |
51 | | - |
52 | | -// const a = { |
53 | | -// useBeforeRouterChange(callback: Function) { |
54 | | -// const router = useRouter(); |
55 | | -// const cacheBeforeChange = router.onBeforeRouteChange; |
56 | | -// router.onBeforeRouteChange = async (to: string) => { |
57 | | -// for (const func of callbackList) { |
58 | | -// await func(to); |
59 | | -// } |
60 | | -// cacheBeforeChange && cacheBeforeChange(to); |
61 | | -// } |
62 | | -// const fn = (callback: Function) => { |
63 | | -// callbackList.push(callback); |
64 | | -// }; |
65 | | -// fn(callback); |
66 | | -// this.useBeforeRouterChange = fn; |
67 | | -// } |
68 | | -// } |
69 | | - |
70 | | -// export { a } |
|
0 commit comments