Header 组件渲染一个 <header> 元素。
使用 left、default 和 right 插槽来自定义页眉,使用 body 或 content 插槽来自定义页眉菜单。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [
{
label: 'Docs',
to: '/docs/getting-started',
active: route.path.startsWith('/docs/getting-started')
},
{
label: 'Components',
to: '/docs/components',
active: route.path.startsWith('/docs/components')
},
{
label: 'Figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
},
{
label: 'Releases',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}
])
</script>
<template>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
</UHeader>
</template>
使用 title prop 来更改页眉的标题。默认为 Nuxt UI。
<template>
<UHeader title="Nuxt UI" />
</template>
您也可以使用 title 插槽来添加您自己的徽标。
<template>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
</UHeader>
</template>
使用 to prop 更改标题的链接。默认为 /。
<template>
<UHeader to="/docs" />
</template>
您也可以使用 left 插槽来完全覆盖链接。
<template>
<UHeader>
<template #left>
<NuxtLink to="/docs">
<Logo class="h-6 w-auto" />
</NuxtLink>
</template>
</UHeader>
</template>
使用 mode prop 更改页眉菜单的模式。默认为 modal。
使用 body 插槽填充菜单正文(页眉下方),或使用 content 插槽填充整个菜单。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
icon: 'i-lucide-book-open',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
icon: 'i-lucide-box',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
icon: 'i-simple-icons-figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
icon: 'i-lucide-rocket',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
</template>
使用 toggle prop 自定义移动设备上显示的切换按钮。
您可以传递 Button 组件的任何属性来自定义它。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
icon: 'i-lucide-book-open',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
icon: 'i-lucide-box',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
icon: 'i-simple-icons-figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
icon: 'i-lucide-rocket',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UHeader
:toggle="{
color: 'primary',
variant: 'subtle',
class: 'rounded-full'
}"
>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
</template>
使用 toggle-side prop 更改切换按钮的侧面。默认为 right。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
icon: 'i-lucide-book-open',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
icon: 'i-lucide-box',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
icon: 'i-simple-icons-figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
icon: 'i-lucide-rocket',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UHeader toggle-side="left">
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UTooltip text="Open on GitHub" :kbds="['meta', 'G']">
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</UTooltip>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
</template>
app.vue 中在您的 app.vue 或布局中使用 Header 组件
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const items = computed<NavigationMenuItem[]>(() => [{
label: 'Docs',
to: '/docs/getting-started',
active: route.path.startsWith('/docs/getting-started')
}, {
label: 'Components',
to: '/docs/components',
active: route.path.startsWith('/docs/components')
}, {
label: 'Figma',
to: 'https://go.nuxt.com/figma-ui',
target: '_blank'
}, {
label: 'Releases',
to: 'https://github.com/nuxt/ui/releases',
target: '_blank'
}])
</script>
<template>
<UApp>
<UHeader>
<template #title>
<Logo class="h-6 w-auto" />
</template>
<UNavigationMenu :items="items" />
<template #right>
<UColorModeButton />
<UButton
color="neutral"
variant="ghost"
to="https://github.com/nuxt/ui"
target="_blank"
icon="i-simple-icons-github"
aria-label="GitHub"
/>
</template>
<template #body>
<UNavigationMenu :items="items" orientation="vertical" class="-mx-2.5" />
</template>
</UHeader>
<UMain>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</UMain>
<UFooter />
</UApp>
</template>
| 属性 | 默认值 | 类型 |
|---|---|---|
as |
|
此组件应渲染为的元素或组件。 |
title |
| |
过渡到 |
|
|
mode |
|
页眉菜单的模式。 |
菜单 |
页眉菜单组件的 props。
| |
toggle |
|
自定义当使用 |
toggleSide |
|
渲染切换按钮的侧边位置。 |
open |
|
|
ui |
|
| 插槽 | 类型 |
|---|---|
title | |
left | |
default | |
right | |
toggle |
|
top | |
bottom | |
主体 |
|
内容 |
|
| 事件 | 类型 |
|---|---|
update:open |
|
export default defineAppConfig({
ui: {
header: {
slots: {
root: 'bg-default/75 backdrop-blur border-b border-default h-(--ui-header-height) sticky top-0 z-50',
container: 'flex items-center justify-between gap-3 h-full',
left: 'lg:flex-1 flex items-center gap-1.5',
center: 'hidden lg:flex',
right: 'flex items-center justify-end lg:flex-1 gap-1.5',
title: 'shrink-0 font-bold text-xl text-highlighted flex items-end gap-1.5',
toggle: 'lg:hidden',
content: 'lg:hidden',
overlay: 'lg:hidden',
header: 'px-4 sm:px-6 h-(--ui-header-height) shrink-0 flex items-center justify-between gap-3',
body: 'p-4 sm:p-6 overflow-y-auto'
},
variants: {
toggleSide: {
left: {
toggle: '-ms-1.5'
},
right: {
toggle: '-me-1.5'
}
}
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
header: {
slots: {
root: 'bg-default/75 backdrop-blur border-b border-default h-(--ui-header-height) sticky top-0 z-50',
container: 'flex items-center justify-between gap-3 h-full',
left: 'lg:flex-1 flex items-center gap-1.5',
center: 'hidden lg:flex',
right: 'flex items-center justify-end lg:flex-1 gap-1.5',
title: 'shrink-0 font-bold text-xl text-highlighted flex items-end gap-1.5',
toggle: 'lg:hidden',
content: 'lg:hidden',
overlay: 'lg:hidden',
header: 'px-4 sm:px-6 h-(--ui-header-height) shrink-0 flex items-center justify-between gap-3',
body: 'p-4 sm:p-6 overflow-y-auto'
},
variants: {
toggleSide: {
left: {
toggle: '-ms-1.5'
},
right: {
toggle: '-me-1.5'
}
}
}
}
}
})
]
})