Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | 1x 1x 1x 1x 1x 1x 7x 1x 1x 1x 1x 1x 66x 66x 66x 7x 7x 59x 59x 19x 19x 2x 2x 19x 19x 6x 6x 13x 19x 8x 8x 5x 5x 5x 3x 3x 8x 8x 1x 7x 7x 1x 4x 4x 49x 49x 49x 35x 35x 14x 14x 14x 1x 28x 28x 1x 21x 21x 1x 50x 50x 50x 50x 50x 50x 50x 50x 19x 18x 18x 50x 50x 50x 1x 21x 21x 21x | import type { Route, Router, RouterLinkProps } from '@esmx/router'; import { type Ref, computed, getCurrentInstance, inject, onBeforeUnmount, provide, ref } from 'vue'; import { createSymbolProperty } from './util'; export interface VueInstance { $parent?: VueInstance | null; $root?: VueInstance | null; $children?: VueInstance[] | null; } interface RouterContext { router: Router; route: Ref<Route>; } const ROUTER_CONTEXT_KEY = Symbol('router-context'); const ROUTER_INJECT_KEY = Symbol('router-inject'); const ERROR_MESSAGES = { SETUP_ONLY: (fnName: string) => `[@esmx/router-vue] ${fnName}() can only be called during setup()`, CONTEXT_NOT_FOUND: '[@esmx/router-vue] Router context not found. ' + 'Please ensure useProvideRouter() is called in a parent component.' } as const; const routerContextProperty = createSymbolProperty<RouterContext>(ROUTER_CONTEXT_KEY); function getCurrentProxy(functionName: string): VueInstance { const instance = getCurrentInstance(); if (!instance || !instance.proxy) { throw new Error(ERROR_MESSAGES.SETUP_ONLY(functionName)); } return instance.proxy; } function findRouterContext(vm?: VueInstance): RouterContext { // If no vm provided, try to get current instance if (!vm) { vm = getCurrentProxy('findRouterContext'); } let context = routerContextProperty.get(vm); if (context) { return context; } let current = vm.$parent; while (current) { context = routerContextProperty.get(current); if (context) { routerContextProperty.set(vm, context); return context; } current = current.$parent; } throw new Error(ERROR_MESSAGES.CONTEXT_NOT_FOUND); } /** * Get router instance from a Vue component instance. * This is a lower-level function used internally by useRouter(). * Use this in Options API, use useRouter() in Composition API. * * @param instance - Vue component instance (optional, will use getCurrentInstance if not provided) * @returns Router instance * @throws {Error} If router context is not found * * @example * ```typescript * // Options API usage * import { defineComponent } from 'vue'; * import { getRouter } from '@esmx/router-vue'; * * export default defineComponent({ * mounted() { * const router = getRouter(this); * router.push('/dashboard'); * }, * methods: { * handleNavigation() { * const router = getRouter(this); * router.replace('/profile'); * } * } * }); * * // Can also be called without instance (uses getCurrentInstance internally) * const router = getRouter(); // Works in globalProperties getters * ``` */ export function getRouter(instance?: VueInstance): Router { return findRouterContext(instance).router; } /** * Get current route from a Vue component instance. * This is a lower-level function used internally by useRoute(). * Use this in Options API, use useRoute() in Composition API. * * @param instance - Vue component instance (optional, will use getCurrentInstance if not provided) * @returns Current route object * @throws {Error} If router context is not found * * @example * ```typescript * // Options API usage * import { defineComponent } from 'vue'; * import { getRoute } from '@esmx/router-vue'; * * export default defineComponent({ * computed: { * routeInfo() { * const route = getRoute(this); * return { * path: route.path, * params: route.params, * query: route.query * }; * } * } * }); * * // Can also be called without instance (uses getCurrentInstance internally) * const route = getRoute(); // Works in globalProperties getters * ``` */ export function getRoute(instance?: VueInstance): Route { return findRouterContext(instance).route.value; } /** * Get router context using the optimal method available. * First tries provide/inject (works in setup), then falls back to hierarchy traversal. */ function useRouterContext(functionName: string): RouterContext { // First try to get context from provide/inject (works in setup) const injectedContext = inject<RouterContext>(ROUTER_INJECT_KEY); if (injectedContext) { return injectedContext; } // Fallback to component hierarchy traversal (works after mount) const proxy = getCurrentProxy(functionName); return findRouterContext(proxy); } /** * Get the router instance in a Vue component. * Must be called within setup() or other composition functions. * Use this in Composition API, use getRouter() in Options API. * * @returns Router instance for navigation and route management * @throws {Error} If called outside setup() or router context not found * * @example * ```vue * <script setup lang="ts"> * import { useRouter } from '@esmx/router-vue'; * * const router = useRouter(); * * const navigateToHome = () => { * router.push('/home'); * }; * * const goBack = () => { * router.back(); * }; * * const navigateWithQuery = () => { * router.push({ * path: '/search', * query: { q: 'vue router', page: '1' } * }); * }; * </script> * ``` */ export function useRouter(): Router { return useRouterContext('useRouter').router; } /** * Get the current route information in a Vue component. * Returns a reactive reference that automatically updates when the route changes. * Must be called within setup() or other composition functions. * Use this in Composition API, use getRoute() in Options API. * * @returns Current route object with path, params, query, etc. * @throws {Error} If called outside setup() or router context not found * * @example * ```vue * <template> * <div> * <h1>{{ route.meta?.title || 'Page' }}</h1> * <p>Path: {{ route.path }}</p> * <p>Params: {{ JSON.stringify(route.params) }}</p> * <p>Query: {{ JSON.stringify(route.query) }}</p> * </div> * </template> * * <script setup lang="ts"> * import { useRoute } from '@esmx/router-vue'; * import { watch } from 'vue'; * * const route = useRoute(); * * watch(() => route.path, (newPath) => { * console.log('Route changed to:', newPath); * }); * </script> * ``` */ export function useRoute(): Route { return useRouterContext('useRoute').route.value; } /** * Provide router context to child components. * This must be called in a parent component to make the router available * to child components via useRouter() and useRoute(). * * @param router - Router instance to provide to child components * @throws {Error} If called outside setup() * * @example * ```typescript * // Vue 3 usage * import { createApp } from 'vue'; * import { Router } from '@esmx/router'; * import { useProvideRouter } from '@esmx/router-vue'; * * const routes = [ * { path: '/', component: () => import('./Home.vue') }, * { path: '/about', component: () => import('./About.vue') } * ]; * * const router = new Router({ routes }); * const app = createApp({ * setup() { * useProvideRouter(router); * } * }); * app.mount('#app'); * ``` */ export function useProvideRouter(router: Router): void { const proxy = getCurrentProxy('useProvideRouter'); const context: RouterContext = { router, route: ref(router.route) as Ref<Route> }; // Provide context via Vue 3's provide/inject (works in setup) provide(ROUTER_INJECT_KEY, context); // Also set on component instance for fallback (works after mount) routerContextProperty.set(proxy, context); const unwatch = router.afterEach((to: Route) => { if (router.route === to) { to.syncTo(context.route.value); } }); onBeforeUnmount(unwatch); } /** * Create reactive link helpers for navigation elements. * Returns computed properties for link attributes, classes, and event handlers. * * @param props - RouterLink properties configuration * @returns Computed link resolver with attributes and event handlers * * @example * ```vue * <template> * <a * v-bind="link.attributes" * v-on="link.getEventHandlers()" * :class="{ active: link.isActive }" * > * Home * </a> * </template> * * <script setup lang="ts"> * import { useLink } from '@esmx/router-vue'; * * const link = useLink({ * to: '/home', * type: 'push', * exact: 'include' * }).value; * </script> * ``` */ export function useLink(props: RouterLinkProps) { const router = useRouter(); return computed(() => router.resolveLink(props)); } |