layouts

Nuxt 提供了一个布局框架,可以将常见的 UI 模式提取到可复用的布局中。
为了获得最佳性能,放置在此目录中的组件在使用时将通过异步导入自动加载。¥For best performance, components placed in this directory will be automatically loaded via asynchronous import when used.

启用布局

¥Enable Layouts

通过将 <NuxtLayout> 添加到你的 app.vue 来启用布局:

¥Layouts are enabled by adding <NuxtLayout> to your app.vue:

app.vue
<template>
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>

要使用布局:

¥To use a layout:

  • 在页面中使用 definePageMeta 设置 layout 属性。
  • 设置 <NuxtLayout>name 属性。
布局名称已规范化为短横线命名,因此 someLayout 变为 some-layout
如果未指定布局,则将使用 layouts/default.vue
如果你的应用中只有一个布局,我们建议你使用 app.vue
与其他组件不同,你的布局必须有一个根元素,才能允许 Nuxt 在布局更改之间应用过渡效果 - 并且此根元素不能是 <slot />

默认布局

¥Default Layout

添加 ~/layouts/default.vue

¥Add a ~/layouts/default.vue:

layouts/default.vue
<template>
  <div>
    <p>Some default layout content shared across all pages</p>
    <slot />
  </div>
</template>

在布局文件中,页面内容将显示在 <slot /> 组件中。

¥In a layout file, the content of the page will be displayed in the <slot /> component.

命名布局

¥Named Layout

Directory Structure
-| layouts/
---| default.vue
---| custom.vue

然后,你可以在页面中使用 custom 布局:

¥Then you can use the custom layout in your page:

pages/about.vue
<script setup lang="ts">
definePageMeta({
  layout: 'custom'
})
</script>

:

Read more in Docs > Guide > Directory Structure > Pages#page Metadata.

了解更多关于 definePageMeta 的信息。

¥Learn more about definePageMeta.

::

你可以使用 <NuxtLayout>name 属性直接覆盖所有页面的默认布局:

¥You can directly override the default layout for all pages using the name property of <NuxtLayout>:

app.vue
<script setup lang="ts">
// You might choose this based on an API call or logged-in status
const layout = "custom";
</script>

<template>
  <NuxtLayout :name="layout">
    <NuxtPage />
  </NuxtLayout>
</template>

如果你的布局位于嵌套目录中,则布局的名称将基于其自身的路径目录和文件名,并删除重复的段。

¥If you have a layout in nested directories, the layout's name will be based on its own path directory and filename, with duplicate segments being removed.

文件布局名称
~/layouts/desktop/default.vuedesktop-default
~/layouts/desktop-base/base.vuedesktop-base
~/layouts/desktop/index.vuedesktop

为了清楚起见,我们建议布局的文件名与其名称匹配:

¥For clarity, we recommend that the layout's filename matches its name:

文件布局名称
~/layouts/desktop/DesktopDefault.vuedesktop-default
~/layouts/desktop-base/DesktopBase.vuedesktop-base
~/layouts/desktop/Desktop.vuedesktop
Read and edit a live example in Docs > Examples > Features > Layouts.

更改动态布局

¥Changing the Layout Dynamically

你还可以使用 setPageLayout 助手动态更改布局:

¥You can also use the setPageLayout helper to change the layout dynamically:

<script setup lang="ts">
function enableCustomLayout () {
  setPageLayout('custom')
}
definePageMeta({
  layout: false,
});
</script>

<template>
  <div>
    <button @click="enableCustomLayout">Update layout</button>
  </div>
</template>
Read and edit a live example in Docs > Examples > Features > Layouts.

逐页覆盖布局

¥Overriding a Layout on a Per-page Basis

如果你使用页面,你可以通过设置 layout: false 然后在页面中使用 <NuxtLayout> 组件来实现完全控制。

¥If you are using pages, you can take full control by setting layout: false and then using the <NuxtLayout> component within the page.

<script setup lang="ts">
definePageMeta({
  layout: false,
})
</script>

<template>
  <div>
    <NuxtLayout name="custom">
      <template #header> Some header template content. </template>

      The rest of the page
    </NuxtLayout>
  </div>
</template>
如果你在页面中使用 <NuxtLayout>,请确保它不是根元素(或 disable layout/page transitions)。