错误组件与 Header 组件协同工作,创建一个延伸至视口可用高度的全高布局。
使用 error 属性显示错误消息。
404
您正在寻找的页面不存在。
<template>
<UError
:error="{
statusCode: 404,
statusMessage: 'Page not found',
message: 'The page you are looking for does not exist.'
}"
/>
</template>
使用 clear 属性来自定义或隐藏清除按钮(设置为 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 属性,在点击清除按钮时将用户重定向到不同的页面。默认为 /。
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 组件
<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.vuenuxt generate 时,建议在您的 createError 调用中添加 fatal: true,以确保显示错误页面<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>
| 属性 | 默认值 | 类型 |
|---|---|---|
as |
|
此组件应渲染为的元素或组件。 |
error |
| |
重定向 |
|
清除错误时重定向的 URL。 |
清除 |
|
在链接插槽中显示清除错误的按钮。 |
ui |
|
| 插槽 | 类型 |
|---|---|
default | |
状态码 | |
状态信息 | |
消息 | |
links |
|
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'
}
}
}
})
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'
}
}
}
})
]
})