layer 的 app.vue 与 error.vue 都以 await useDocsShell() 开头。如果你在项目中覆盖了它们,必须同样调用,否则 useDocsNavigation、页头栏目条和侧栏都拿不到导航数据。
用法
app/app.vue
<script setup lang="ts">
const { nuxtUiLocale, rootNavigation, files } = await useDocsShell()
</script>
<template>
<UApp :locale="nuxtUiLocale">
<Header />
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
<Footer />
<ClientOnly>
<UContentSearch :files="files" :navigation="rootNavigation" />
</ClientOnly>
</UApp>
</template>
上例只保留了外壳的最小结构。layer 的 app.vue 还挂载了 AI Chat 面板、⌘K 最近访问分组与 useLocaleSeo(),重写时请以它为起点复制后修改。
签名
function useDocsShell(): Promise<{
nuxtUiLocale: ComputedRef<Locale>
rootNavigation: ComputedRef<ContentNavigationItem[]>
files: Ref<ContentSearchSection[] | undefined>
}>
返回值
| 名称 | 类型 | 说明 |
|---|---|---|
nuxtUiLocale | ComputedRef<Locale> | 当前语言对应的 @nuxt/ui/locale 语言包,找不到时回退为 en;传给 UApp 的 locale |
rootNavigation | ComputedRef<ContentNavigationItem[]> | 顶层栏目导航,同时以 'navigation' 为 key 提供给组件树 |
files | Ref<ContentSearchSection[] | undefined> | 当前语言的全文搜索索引,仅在客户端懒加载,传给 UContentSearch |
副作用
调用后会注册:
- 按语言区分 key 的导航请求(
docs-navigation-{locale}),查询时带上页面的category与description字段。 <html>的lang、dir,theme.font为sans时的data-font="sans",theme.grain开启时的data-grain。viewport与theme-colormeta,以及useTheme生成的圆角、黑色主色样式。
注意事项
- 必须在
<script setup>顶层await。函数内部在唯一的await之前完成provide与useHead,调用方不要在它之前插入其他await。 - 多语言 SEO 不在外壳内,需要另外调用
useLocaleSeo()。
更新日志
No recent changes