Skip to main content

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:
Source: 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:
Source: packages/vue-router/src/RouterProvider.tsx:1-98

<Link>

Creates type-safe navigation links with automatic active states:
Source: packages/vue-router/src/link.tsx:709-792

<Outlet>

Renders child route components:
Source: packages/vue-router/src/Match.tsx

Router Hooks

All hooks return Vue refs for automatic reactivity:

useRouter

Access the router instance:
Source: 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:
Source: packages/vue-router/src/route.ts

Loader Context

Loaders receive context with params, search, and more:

Error Handling

Handle errors with error components:
Source: packages/vue-router/src/CatchBoundary.tsx

Programmatic Navigation

Automatically preload routes on hover, focus, or when visible:
Source: 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: Example:
Source: See 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:
This enables:
  • 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:
For full SSR support, consider using TanStack Start, the full-stack framework built on TanStack Router.

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:

API Reference

For detailed API documentation, see:

Examples

Explore example applications in the repository:

Resources