Vue Router
TanStack Router provides first-class support for Vue 3 with reactive composables, components, and patterns designed specifically for Vue applications.Installation
Core Components
RouterProvider
The top-level component that renders active route matches and provides the router to the Vue tree.Link
Reactive navigation component with preloading and active state support.to- Type-safe destination pathparams- Path parameterssearch- Search parametersactiveProps- Props applied when link is activeinactiveProps- Props applied when link is inactivepreload- Controls preloading behavior (false,'intent','viewport')disabled- Disables navigation
Outlet
Renders child route matches at the current level.ErrorComponent
Default error boundary component that displays error details.Navigate
Component-based navigation trigger.Vue Composables
useRouter
Access the router instance from any component.useNavigate
Type-safe programmatic navigation.useParams
Access route path parameters reactively.useParams returns a reactive ref, so access with params.value.
useSearch
Access route search parameters reactively.useLoaderData
Access data loaded by the route’s loader function as a reactive ref.useLoaderData returns a ref, so access data with post.value.
useMatch
Access the current route match with reactive properties.useRouteContext
Access route context provided by parent routes reactively.useLocation
Access the current location object reactively.useRouterState
Access router state reactively with optional selector.isLoading.value.
useMatchRoute
Check if a specific route matches the current location.useLinkProps
Build anchor-like props for custom link components.useBlocker
Block navigation based on a condition (useful for unsaved changes).Route API
RouteApi
Route-specific API with pre-bound reactive composables for a specific route ID.Creating Routes
Code-based Routes
File-based Routes (JSX)
File-based Routes (SFC)
Vue Router also supports.vue Single File Components:
SSR Support
Vue Router includes specialized SSR utilities:Vue-Specific Features
Reactive Refs
All router state is returned as Vue refs:Single File Component Support
Vue Router fully supports.vue SFC syntax:
Template Syntax
Use Vue’s template directives with router components:Computed Properties
Combine router state with computed properties:Complete Example
Best Practices
-
Type Safety: Always register your router type:
-
Use Refs: Remember that composables return refs:
-
Watch Changes: Use Vue’s
watchfor side effects: -
SFC Support: Leverage
.vuefiles for better DX: -
Template Directives: Use
v-if,v-for, etc. with router state:
Key Differences from React Router
- Refs: All composables return Vue refs, not plain values - access with
.value - SFC Support: Full support for
.vueSingle File Components - Template Syntax: Can use Vue template directives with router components
- Watch API: Use Vue’s
watchinstead ofuseEffectfor side effects - No Hooks Rules: Vue composition API is more flexible than React hooks
- JSX Support: Supports both JSX and template syntax