错误
一个预构建的错误组件,支持 NuxtError。
用法
错误组件渲染一个 `<main>
` 元素,它与 Header 组件配合使用,创建了一个全屏布局,该布局会延伸到视口的可使用高度。
错误组件使用 `--ui-header-height` CSS 变量来正确地定位在 Header 下方。
错误
使用 `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.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/v4/docs/app/error.vue
您可以阅读更多关于如何在 Nuxt 文档中处理错误的信息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 |
|
此组件应渲染为的元素或组件。 |
error |
| |
redirect |
|
清除错误时重定向的 URL。 |
clear |
|
在 links 插槽中显示一个清除错误的按钮。使用 `size`、`color`、`variant` 和 `label` prop 来配置按钮。例如: |
ui |
|
插槽
插槽 | 类型 |
---|---|
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'
}
}
}
})
]
})
更新日志
5cb65
— 特性:导入 @nuxt/ui-pro
组件