TanStack Vue Router
TanStack Router provides first-class support for Vue 3 with a reactive, type-safe routing solution. It integrates seamlessly with Vue’s composition API and provides built-in caching, preloading, and URL state management.Installation
Install the Vue Router package:Quick Start
Here’s a minimal example to get started with TanStack Router in a Vue application:packages/vue-router/src/router.ts:75-77
Core Concepts
Reactive Data with Refs
Vue Router integrates with Vue’s reactivity system. Data returned from hooks are wrapped in refs for automatic reactivity:Router Components
Vue Router provides reactive components that work with Vue’s JSX and SFC:<RouterProvider>
The top-level component that provides the router to your application:
packages/vue-router/src/RouterProvider.tsx:1-98
<Link>
Creates type-safe navigation links with automatic active states:
packages/vue-router/src/link.tsx:709-792
<Outlet>
Renders child route components:
packages/vue-router/src/Match.tsx
Router Hooks
All hooks return Vue refs for automatic reactivity:useRouter
Access the router instance:
packages/vue-router/src/useRouter.tsx:6-15
useNavigate
Get a type-safe navigate function:
useParams
Access route parameters:
useSearch
Access search parameters:
useLoaderData
Access loader data for the current route:
useRouterState
Access reactive router state:
Data Loading
Route Loaders
Define data loading at the route level:packages/vue-router/src/route.ts
Loader Context
Loaders receive context with params, search, and more:Error Handling
Handle errors with error components:packages/vue-router/src/CatchBoundary.tsx
Navigation
Programmatic Navigation
Link Preloading
Automatically preload routes on hover, focus, or when visible:packages/vue-router/src/link.tsx:115-124
File-Based Routing
TanStack Router supports file-based routing for Vue with both JSX and Single File Components (SFC).Setup
Route Files with JSX
Create routes using JSX components:Route Files with SFC
Use Vue Single File Components with the file-based routing convention documented in the Vue Router README. File-Based Routing Conventions:| Pattern | Purpose |
|---|---|
.route.ts | Route configuration (loader, validateSearch, etc.) |
.component.vue | The component rendered for the route |
.errorComponent.vue | Error boundary component |
.notFoundComponent.vue | Not found component |
.lazy.ts | Lazy-loaded route configuration |
_layout prefix | Layout routes that wrap children |
$param | Dynamic route parameters |
packages/vue-router/README.md for the full file-based routing table
Source: packages/vue-router/src/fileRoute.ts
Type Safety
Register your router for full type safety across your application:- Autocomplete for route paths
- Type-safe params and search parameters
- Type-safe loader data
- Type-safe navigation
SSR Support
TanStack Vue Router supports server-side rendering:DevTools
Add the TanStack Router DevTools to inspect routing state during development:Advanced Features
Search Parameter Validation
Validate and parse search parameters with schemas:Route Context
Share data between parent and child routes:Lazy Loading
Lazy load route components for code splitting:Migration from Vue Router
If you’re migrating from Vue Router, here are the key differences:| Vue Router | TanStack Router |
|---|---|
createRouter() | createRouter() with route tree |
<router-view> | <Outlet> |
<router-link> | <Link> |
useRoute() | useParams(), useSearch() |
useRouter() | useRouter() |
| Route guards | beforeLoad, loaders |
| Nested routes in config | getParentRoute() |
API Reference
For detailed API documentation, see:- Router API - Core router APIs apply to Vue
- Route API - Route creation and configuration
- Link API - Navigation components
- Hooks API - Available hooks