Composables
Introduction
@esmx/router-vue provides Composition API functions for accessing the router and current route within Vue components. These composables must be called inside setup() or other composition functions. For Options API usage, lower-level functions (getRouter, getRoute, getRouterViewDepth) are also available.
useProvideRouter()
- Parameters:
router: Router— Router instance to provide
- Returns:
void - Throws:
Error— If called outsidesetup()
Provides the router context to all descendant components. Must be called in a root or parent component's setup(). Sets up reactive proxies for both the router and the current route, ensuring that Vue components reactively update when the route changes.
useRouter()
- Returns:
Router - Throws:
Error— If called outsidesetup()or if router context is not found
Gets the router instance in a Vue component's Composition API. Must be called within setup().
useRoute()
- Returns:
Route - Throws:
Error— If called outsidesetup()or if router context is not found
Gets the current reactive route object. The component automatically re-renders when the route changes.
useLink()
- Parameters:
props: RouterLinkProps— Link configuration
- Returns:
ComputedRef<RouterLinkResolved>
Creates reactive link helpers for navigation elements. Returns a computed reference that updates when the route changes.
useRouterViewDepth()
- Returns:
number - Throws:
Error— If called outsidesetup()
Gets the current RouterView nesting depth. Returns 0 for root level, 1 for the first nested level, and so on.
Lower-Level Functions
getRouter()
- Parameters:
instance: VueInstance— Vue component instance
- Returns:
Router - Throws:
Error— If router context is not found
Gets the router instance from a Vue component instance. Use this in Options API; use useRouter() in Composition API.
getRoute()
- Parameters:
instance: VueInstance— Vue component instance
- Returns:
Route - Throws:
Error— If router context is not found
Gets the current route from a Vue component instance. Use this in Options API; use useRoute() in Composition API.
getRouterViewDepth()
- Parameters:
instance: VueInstance— Vue component instance
- Returns:
number
Gets the RouterView depth from a Vue component instance by traversing the parent chain. Use this in Options API; use useRouterViewDepth() in Composition API. Returns 0 if no RouterView ancestor is found.