布局

Nuxt Kit 提供了一组实用程序来帮助你使用布局。

布局用于封装你的页面。它可以用来封装页面,其中包含常用组件,例如页眉和页脚。可以使用 addLayout 实用程序注册布局。

¥Layouts is used to be a wrapper around your pages. It can be used to wrap your pages with common components, for example, a header and a footer. Layouts can be registered using addLayout utility.

addLayout

将模板注册为布局并将其添加到布局中。

¥Register template as layout and add it to the layouts.

在 Nuxt 2 中,也可以使用此实用程序注册 error 布局。在 Nuxt 3+ 中,error 布局 replacederror.vue 页面位于项目根目录中。

类型

¥Type

function addLayout (layout: NuxtTemplate | string, name: string): void

interface NuxtTemplate {
  src?: string
  filename?: string
  dst?: string
  options?: Record<string, any>
  getContents?: (data: Record<string, any>) => string | Promise<string>
  write?: boolean
}

参数

¥Parameters

layout

类型:NuxtTemplate | string

¥Type: NuxtTemplate | string

必需:true

¥Required: true

模板对象或包含模板路径的字符串。如果提供了字符串,它将被转换为模板对象,并将 src 设置为该字符串值。如果提供了模板对象,它必须具有以下属性:

¥A template object or a string with the path to the template. If a string is provided, it will be converted to a template object with src set to the string value. If a template object is provided, it must have the following properties:

  • src(可选)
    类型:string
    模板的路径。如果未提供 src,则必须提供 getContents
  • filename(可选)
    类型:string
    模板文件名。如果未提供 filename,则它将从 src 路径生成。在这种情况下,src 选项是必需的。
  • dst(可选)
    类型:string
    目标文件的路径。如果未提供 dst,则它将从 filename 路径和 nuxt buildDir 选项生成。
  • options(可选)
    类型:Options
    传递给模板的选项。
  • getContents(可选)
    类型:(data: Options) => string | Promise<string>
    一个函数,用于使用 options 对象调用。它应该返回一个字符串或一个解析为字符串的 Promise。如果提供了 src,则此函数将被忽略。
  • write(可选)
    类型:boolean
    如果设置为 true,模板将被写入目标文件。否则,该模板将仅在虚拟文件系统中使用。