Skip to main content

Composables

VitePress provides several Vue composables that give you access to app data, routing, and theme configuration.

Core Composables

useData

Access page-level and site-level data.
Returns: VitePressData<T>
Ref<SiteData<T>>
Site-level metadata and configuration
Ref<T>
The themeConfig from .vitepress/config.js
Ref<PageData>
Current page metadata including frontmatter, headers, and file paths
Ref<Record<string, any>>
Frontmatter data for the current page
Ref<Record<string, any>>
Dynamic route parameters (for data loading)
Ref<string>
Current page title (computed from frontmatter or first h1)
Ref<string>
Current page description
Ref<string>
Current language code
Ref<string>
Text direction (ltr or rtl)
Ref<string>
Current locale index (e.g., 'root', 'zh', 'en')
Ref<boolean>
Whether dark mode is currently active
Ref<string>
Current location hash (e.g., #introduction)
Example Usage:

useRoute

Access the current route information.
Returns: Route (reactive object)
string
Current URL path (e.g., /guide/introduction.html)
string
Current URL hash (e.g., #heading-anchor)
string
Current URL query string (e.g., ?tab=1&view=grid)
PageData
Current page data object
Component | null
Current page component
Example Usage:

useRouter

Access the router instance for programmatic navigation.
Returns: Router
Route
Current route object (same as useRoute())
(to: string, options?) => Promise<void>
Navigate to a new URL
Navigation Options:
string
required
The URL to navigate to
boolean
default:"false"
Whether to smoothly scroll to the target position
boolean
default:"false"
Whether to replace the current history entry instead of pushing a new one
Router Hooks:
(to: string) => Awaitable<void | boolean>
Called before the route changes. Return false to cancel navigation.
(to: string) => Awaitable<void | boolean>
Called before the page component loads. Return false to cancel.
(to: string) => Awaitable<void>
Called after the page component loads but before update.
(to: string) => Awaitable<void>
Called after the route changes.
Example Usage:

Type Definitions

VitePressData

Route

Router

PageData

SiteData


Usage Tips

Reactive Updates: All data returned from composables is reactive. Use Vue’s watch or watchEffect to respond to changes.
SSR Compatibility: When using these composables, ensure your code handles SSR correctly. Use onMounted for browser-only logic.