ErrorPRO

一个内置的错误组件,支持 NuxtError。

用法

Main 组件类似,Error 组件渲染一个 <main> 元素,它与 Header 组件协同工作,创建一个全高布局,该布局会延伸到视口的可用高度。

Header 组件通过一个 --ui-header-height CSS 变量定义其高度,你可以在你的 CSS 中覆盖它来定制。
:root {
  --ui-header-height: --spacing(16);
}

错误 prop

使用 error prop 来显示错误消息。

在大多数情况下,你会在你的 error.vue 文件中接收到 error prop。
<template>
  <UError
    :error="{
      statusCode: 404,
      statusMessage: 'Page not found',
      message: 'The page you are looking for does not exist.'
    }"
  />
</template>

clear prop

使用 clear prop 来定制或隐藏清除按钮(使用 false 值)。

你可以传递 Button 组件的任何属性来定制它。

<template>
  <UError
    :clear="{
      color: 'neutral',
      size: 'xl',
      icon: 'i-lucide-arrow-left',
      class: 'rounded-full'
    }"
    :error="{
      statusCode: 404,
      statusMessage: 'Page not found',
      message: 'The page you are looking for does not exist.'
    }"
  />
</template>

redirect prop

使用 redirect prop,在点击清除按钮时将用户重定向到另一个页面。默认为 /

<template>
  <UError
    redirect="/getting-started"
    :error="{
      statusCode: 404,
      statusMessage: 'Page not found',
      message: 'The page you are looking for does not exist.'
    }"
  />
</template>

示例

error.vue

在你的 error.vue 中使用 Error 组件

error.vue
<script setup lang="ts">
import type { NuxtError } from '#app'

const props = defineProps<{
  error: NuxtError
}>()
</script>

<template>
  <UApp>
    <UHeader />

    <UError :error="error" />

    <UFooter />
  </UApp>
</template>
你可能希望复制你的 app.vue 文件中的代码到你的 error.vue 文件中,以获得相同的布局和功能,这里有一个示例:https://github.com/nuxt/ui/blob/v3/docs/app/error.vue
你可以在Nuxt 文档中阅读更多关于如何处理错误的信息,但在使用 nuxt generate 时,建议在你的 createError 调用中添加 fatal: true 以确保显示错误页面。
pages/[...slug].vue
<script setup lang="ts">
const route = useRoute()

const { data: page } = await useAsyncData(route.path, () => queryContent(route.path).findOne())
if (!page.value) {
  throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
}
</script>

API

属性

属性默认值类型
as

'main'

any

clear

true

boolean | Partial<ButtonProps>

redirect

'/'

string

error

Partial<NuxtError<unknown> & { message: string; }>

ui

{ root?: ClassNameValue; statusCode?: ClassNameValue; statusMessage?: ClassNameValue; message?: ClassNameValue; links?: ClassNameValue; }

插槽

插槽类型
default

{}

statusCode

{}

statusMessage

{}

links

{}

主题

app.config.ts
export default defineAppConfig({
  uiPro: {
    error: {
      slots: {
        root: 'min-h-[calc(100vh-var(--ui-header-height))] flex flex-col items-center justify-center text-center',
        statusCode: 'text-base font-semibold text-primary',
        statusMessage: 'mt-2 text-4xl sm:text-5xl font-bold text-highlighted text-balance',
        message: 'mt-4 text-lg text-muted text-balance',
        links: 'mt-8 flex items-center justify-center gap-6'
      }
    }
  }
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'

export default defineConfig({
  plugins: [
    vue(),
    ui({
      uiPro: {
        error: {
          slots: {
            root: 'min-h-[calc(100vh-var(--ui-header-height))] flex flex-col items-center justify-center text-center',
            statusCode: 'text-base font-semibold text-primary',
            statusMessage: 'mt-2 text-4xl sm:text-5xl font-bold text-highlighted text-balance',
            message: 'mt-4 text-lg text-muted text-balance',
            links: 'mt-8 flex items-center justify-center gap-6'
          }
        }
      }
    })
  ]
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import uiPro from '@nuxt/ui-pro/vite'

export default defineConfig({
  plugins: [
    vue(),
    uiPro({
      uiPro: {
        error: {
          slots: {
            root: 'min-h-[calc(100vh-var(--ui-header-height))] flex flex-col items-center justify-center text-center',
            statusCode: 'text-base font-semibold text-primary',
            statusMessage: 'mt-2 text-4xl sm:text-5xl font-bold text-highlighted text-balance',
            message: 'mt-4 text-lg text-muted text-balance',
            links: 'mt-8 flex items-center justify-center gap-6'
          }
        }
      }
    })
  ]
})