Error

NuxtErrorをサポートするビルド済みのエラーコンポーネント。

使用法

Errorコンポーネントは<main>要素をレンダリングし、Headerコンポーネントと連携してビューポートの使用可能な高さまで拡張するフルハイトレイアウトを作成します。

ErrorコンポーネントはCSS変数--ui-header-heightを使用して、Headerの下に正しく配置します。

Error

errorプロパティを使用してエラーメッセージを表示します。

ほとんどの場合、error.vueファイルのerrorプロパティを受け取ります。

404

Page not found

The page you are looking for does not exist.

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

アイコンbadge{label="4.8+" class="align-text-top"}

iconプロパティを使用して、ステータスコードの上にアイコンを表示します。

404

Page not found

The page you are looking for does not exist.

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

#leadingスロットを使用して、ロゴなどのカスタム要素を表示します。

404

Page not found

The page you are looking for does not exist.

<template>
  <UError
    :error="{
      statusCode: 404,
      statusMessage: 'Page not found',
      message: 'The page you are looking for does not exist.'
    }"
  >
    <template #leading>
      <img src="https://github.com/nuxt.png" alt="Logo" class="size-10 rounded-full" />
    </template>
  </UError>
</template>

クリア

clearプロパティを使用して、クリアボタン(false値)をカスタマイズまたは非表示にします。

Buttonコンポーネントの任意のプロパティを渡してカスタマイズできます。

404

Page not found

The page you are looking for does not exist.

<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

redirectプロパティを使用して、clearボタンがクリックされたときにユーザーを別のページにリダイレクトします。デフォルトは/です。

404

Page not found

The page you are looking for does not exist.

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

例

xerror.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

Props

Prop Default Type
as
'main'
any

The element or component this component should render as.

icon
any

The icon displayed above the status code.

error
Partial<NuxtError<unknown> & { message: string; }>
redirect
'/'
string

The URL to redirect to when the error is cleared.

clear
true
boolean | ButtonProps

Display a button to clear the error in the links slot. { size: 'lg', color: 'primary', variant: 'solid', label: 'Back to home' }

ui
{ root?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; statusCode?: SlotClass; statusMessage?: SlotClass; message?: SlotClass; links?: SlotClass; }

スロット

Slot Type
default
{}
leading
{ ui: object; }
statusCode
{}
statusMessage
{}
message
{}
links
{}

Theme

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',
        leading: 'mb-4 flex items-center justify-center',
        leadingIcon: 'size-10 shrink-0 text-primary',
        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',
            leading: 'mb-4 flex items-center justify-center',
            leadingIcon: 'size-10 shrink-0 text-primary',
            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'
          }
        }
      }
    })
  ]
})

Changelog

No recent changes