error.vue
error.vue 檔案是您的 Nuxt 應用程式中的錯誤頁面。
在您的應用程式的生命週期中,有些錯誤可能會在運行時意外出現。在這種情況下,我們可以使用 error.vue
檔案來覆蓋預設的錯誤檔案,並以更友好的方式顯示錯誤。
error.vue
<script setup lang="ts">
import type { NuxtError } from '#app'
const props = defineProps({
error: Object as () => NuxtError
})
</script>
<template>
<div>
<h1>{{ error.statusCode }}</h1>
<NuxtLink to="/">Go back home</NuxtLink>
</div>
</template>
雖然它被稱為「錯誤頁面」,但它不是一個路由,不應放置在您的
~/pages
目錄中。基於同樣的原因,您不應在此頁面中使用 definePageMeta
。話雖如此,您仍然可以在錯誤檔案中使用版面配置,方法是使用 NuxtLayout
元件並指定版面配置的名稱。錯誤頁面有一個單一的 prop - error
,其中包含一個錯誤供您處理。
error
物件提供以下欄位
{
statusCode: number
fatal: boolean
unhandled: boolean
statusMessage?: string
data?: unknown
cause?: unknown
}
如果您有一個帶有自訂欄位的錯誤,它們將會遺失;您應該將它們分配給 data
來代替
throw createError({
statusCode: 404,
statusMessage: 'Page Not Found',
data: {
myCustomField: true
}
})