错误

GitHub
一个预构建的错误组件,支持 NuxtError。

用法

错误组件与头部组件协同工作,创建一个延伸至视口可用高度的全高布局。

错误组件使用 --ui-header-height CSS 变量来正确定位在头部下方。

错误

使用 error prop 显示错误信息。

在大多数情况下,你会在 error.vue 文件中收到 error prop。

404

未找到页面

您正在查找的页面不存在。

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

清除

使用 clear prop 来自定义或隐藏清除按钮(设置为 false)。

您可以传递 Button 组件的任何属性来自定义它。

404

未找到页面

您正在查找的页面不存在。

<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 在点击清除按钮时将用户重定向到不同的页面。默认为 /

404

未找到页面

您正在查找的页面不存在。

<template>
  <UError
    redirect="/docs/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>
你可能希望在你的 error.vue 文件中复制 app.vue 的代码,以获得相同的布局和功能,示例如下https://github.com/nuxt/ui/blob/v4/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, () => {
  return queryCollection('docs').path(route.path).first()
})
if (!page.value) {
  throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
}
</script>

API

属性

属性默认值类型
as'div'any

此组件应渲染为的元素或组件。

errorPartial<NuxtError<unknown> & { message: string; }>
重定向'/'string

清除错误后重定向的 URL。

清除trueboolean | ButtonProps

在链接槽中显示一个按钮以清除错误。 { size: 'lg', color: 'primary', variant: 'solid', label: 'Back to home' }

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

插槽

插槽类型
default{}
statusCode{}
statusMessage{}
message{}
links{}

主题

app.config.ts
export default defineAppConfig({
  ui: {
    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({
      ui: {
        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'
          }
        }
      }
    })
  ]
})

更新日志

184ea— chore: 减少类型冗余,通过省略操作按钮中的链接属性

dd81d— feat: 添加 data-slot 属性 (#5447)

2a09a— 修复:渲染为 div 而非 main

5cb65— 特性:导入 @nuxt/ui-pro 组件