---
title: "error.vue"
description: "error.vue 文件是你 Nuxt 应用中的错误页面。"
canonical_url: "https://nuxt.zhcndoc.com/docs/4.x/directory-structure/app/error"
---
# error.vue

> error.vue 文件是你 Nuxt 应用中的错误页面。

在应用的生命周期中，某些错误可能会在运行时意外出现。在这种情况下，我们可以使用 `error.vue` 文件来覆盖默认的错误文件并以更友好的方式显示错误。

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

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

<template>
  <div>
    <h1>{{ error.status }}</h1>
    <NuxtLink to="/">Go back home</NuxtLink>
  </div>
</template>
```

::note
虽然它被称为“错误页面”，但它并不是一个路由，也不应该放在你的 `~/pages` 目录中。出于同样的原因，你不应该在此页面中使用 `definePageMeta`。话虽如此，你仍然可以在错误文件中使用布局，只需使用 [`NuxtLayout`](https://nuxt.zhcndoc.com/docs/4.x/api/components/nuxt-layout) 组件并指定布局名称即可。
::

错误页面有一个单一的 prop —— `error`，其中包含需要你处理的错误。

`error` 对象提供以下字段：

```ts
interface NuxtError {
  status: number
  fatal: boolean
  unhandled: boolean
  statusText?: string
  data?: unknown
  cause?: unknown
  // legacy/deprecated equivalent of `status`
  statusCode: number
  // legacy/deprecated equivalent of `statusText`
  statusMessage?: string
}
```

如果你的错误包含自定义字段，它们将会丢失；你应该把它们分配到 `data` 中：

```ts
throw createError({
  status: 404,
  statusText: 'Page Not Found',
  data: {
    myCustomField: true,
  },
})
```


## Sitemap

See the full [sitemap](https://nuxt.zhcndoc.com/sitemap.md) for all pages.
