Fetch data asynchronously with automatic caching and SSR support.
const { data, pending, error, refresh } = await useAsyncData(
'unique-key',
() => queryCollection('posts').all()
)
Parameters:
key (string): Unique identifier for cachinghandler (function): Async function that returns dataReturns:
data: Reactive data referencepending: Loading stateerror: Error staterefresh: Function to refetch dataSet SEO meta tags for the current page.
useSeoMeta({
title: 'Page Title',
ogTitle: 'Page Title',
description: 'Page description',
ogDescription: 'Page description',
ogImage: 'https://example.com/image.jpg'
})
Access the current route information.
const route = useRoute()
console.log(route.path) // Current path
console.log(route.params) // Route parameters
console.log(route.query) // Query parameters
Navigate programmatically.
const router = useRouter()
// Navigate to a route
router.push('/api-docs')
// Navigate with parameters
router.push({
path: '/api-docs',
query: { section: 'api' }
})
// Go back
router.back()
Manage color mode (light/dark theme).
const colorMode = useColorMode()
// Get current mode
console.log(colorMode.value) // 'light' or 'dark'
// Set mode
colorMode.preference = 'dark'
// Toggle mode
const toggleDark = () => {
colorMode.preference = colorMode.value === 'dark' ? 'light' : 'dark'
}
Query content collections with filtering and sorting.
// Get all items
const posts = await queryCollection('posts').all()
// Get first item
const page = await queryCollection('pages').first()
// Filter by path
const doc = await queryCollection('apiDocs')
.path('/api-docs/getting-started')
.first()
// Order results
const versions = await queryCollection('versions')
.order('date', 'DESC')
.all()
Get previous and next items in a collection.
const surround = await queryCollectionItemSurroundings(
'apiDocs',
route.path,
{
fields: ['title', 'description']
}
)
Define page-level metadata and configuration.
definePageMeta({
layout: 'docs',
middleware: 'auth'
})
Navigate to a URL.
// Navigate to internal route
await navigateTo('/api-docs')
// Navigate to external URL
await navigateTo('https://example.com', {
external: true
})
// Replace current route
await navigateTo('/api-docs', {
replace: true
})
Create and throw an error.
throw createError({
statusCode: 404,
statusMessage: 'Page not found',
fatal: true
})
interface ContentNavigationItem {
title: string
_path: string
children?: ContentNavigationItem[]
icon?: string
badge?: string
}
interface PageData {
title: string
description: string
body?: any
seo?: {
title?: string
description?: string
}
navigation?: {
icon?: string
}
}
<script setup lang="ts">
const { data: posts } = await useAsyncData('blog-posts', () =>
queryCollection('posts')
.order('date', 'DESC')
.all()
)
</script>
<template>
<div>
<UCard
v-for="post in posts"
:key="post._path"
:title="post.title"
:description="post.description"
/>
</div>
</template>
<script setup lang="ts">
const route = useRoute()
const { data: page } = await useAsyncData(route.path, () =>
queryCollection('apiDocs').path(route.path).first()
)
useSeoMeta({
title: page.value?.title,
description: page.value?.description
})
</script>
<script setup lang="ts">
const router = useRouter()
const navigateWithState = async () => {
await router.push({
path: '/api-docs',
query: {
section: 'api',
highlight: 'composables'
}
})
}
</script>