useHeader
useHeader() resolves header.nav / header.mobileNav from app.config into links ready to render for the current locale. HeaderCenter renders desktopLinks and the mobile drawer HeaderBody renders mobileLinks.
Recommended: Declare in app.config
Most sites never touch this function. Declare the links in app.config, see Navigation and Categories:
export default defineAppConfig({
header: {
nav: [
{ label: { 'zh-CN': '文档', 'en': 'Docs' }, to: '/docs/getting-started', activeMatch: '/docs' },
{ label: 'GitHub', to: 'https://github.com/owner/repo' }
]
}
})
Signature
function useHeader(): {
desktopLinks: ComputedRef<NavLink[]>
mobileLinks: ComputedRef<NavLink[]>
}
interface NavLink {
label: string
to: string
icon?: string
target?: '_blank' | '_self'
active: boolean
}
Return Value
| Name | Type | Description |
|---|---|---|
desktopLinks | ComputedRef<NavLink[]> | Desktop links resolved from header.nav |
mobileLinks | ComputedRef<NavLink[]> | Mobile links resolved from header.mobileNav; falls back to header.nav when mobileNav is empty |
Resolution rules:
- When
labelis a per-locale object, the current locale is used first, then the default locale, then the first value. - Internal links get the locale prefix through
localePath; external links are kept as-is and default totarget: '_blank'. - An explicit
activewins; otherwise an internal link is active when the current path starts withactiveMatch(defaults toto). External links are never active.
Overriding
When navigation depends on login state, remote data and so on, create a same-named file in your project. Project composables take precedence over the layer's auto-import:
export function useHeader() {
const route = useRoute()
const { localePath } = useMovkI18n()
const desktopLinks = computed(() => [
{ label: 'Docs', to: localePath('/docs/getting-started'), active: route.path.startsWith(localePath('/docs')) },
{ label: 'Blog', to: 'https://example.com/blog', target: '_blank' as const, active: false }
])
return {
desktopLinks,
mobileLinks: desktopLinks
}
}
An override must return both desktopLinks and mobileLinks, with items shaped like NavLink. Once overridden, header.nav / header.mobileNav no longer apply.
Related
Changelog
useNavigation
Shape the docs navigation tree for the sidebar - top-level sections, the current section's pages grouped by docs.categories, and breadcrumbs for any path. Use it in a custom sidebar or table of contents to keep the default grouping rules.
useCategory
Provides the sidebar category definitions that decide how pages with a category are grouped and ordered within a section. Declare them in docs.categories in app.config; override the composable in your project only when categories must be computed at runtime.