본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Error

원문 보기 위키 갱신

NuxtError(Nuxt의 에러 객체)를 지원하는 미리 만들어진 에러 컴포넌트예요. 아이콘, 정리(clear) 버튼, 리다이렉션 경로까지 손쉽게 커스터마이징할 수 있어요.

출처: Nuxt UI

본문

NuxtError(Nuxt의 에러 객체)를 지원하는 미리 만들어진 에러 컴포넌트예요.

사용법

Error 컴포넌트는 <main> 요소를 렌더링하는데요, Header 컴포넌트와 함께 사용해서 뷰포트의 사용 가능한 높이까지 채우는 전체 높이 레이아웃을 만들어요.

[!TIP] See: /docs/getting-started/theme/css-variables#header

The Error component uses the --ui-header-height CSS variable to position itself correctly below the Header.

Error 컴포넌트는 --ui-header-height CSS 변수를 사용해서 Header 아래에 올바르게 위치해요.

에러 (Error)

error prop으로 에러 메시지를 표시할 수 있어요.

Nuxt:

[!NOTE] See: https://nuxt.com/docs/guide/directory-structure/error

In most cases, you will receive the error prop in your error.vue file.

대부분의 경우 error.vue 파일에서 error prop을 받게 돼요.

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

아이콘 (Icon) 4.8+

icon prop으로 상태 코드 위에 아이콘을 표시할 수 있어요.

<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 슬롯을 사용해서 로고 같은 커스텀 요소를 표시할 수도 있어요.

<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)

clear prop으로 정리 버튼을 커스터마이징하거나(false 값으로) 숨길 수 있어요.

Button 컴포넌트의 어떤 속성이든 전달해서 커스터마이징할 수 있어요.

<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 prop으로 정리 버튼을 클릭했을 때 사용자를 다른 페이지로 이동시킬 수 있어요. 기본값은 /이에요.

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

예시 (Examples)

error.vue 안에서 (Within 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>

[!TIP]

You might want to replicate the code of your app.vue inside your error.vue file to have the same layout and features, here is an example: https://github.com/nuxt/ui/blob/v4/docs/app/error.vue

같은 레이아웃과 기능을 갖추려면 app.vue의 코드를 error.vue 파일 안에 복제하는 걸 고려해 볼 수 있어요. 예시는 여기 있어요: https://github.com/nuxt/ui/blob/v4/docs/app/error.vue

[!NOTE]

You can read more about how to handle errors in the Nuxt documentation, but when using nuxt generate it is recommended to add fatal: true inside your createError call to make sure the error page is displayed:

에러 처리 방법에 대한 자세한 내용은 Nuxt 문서에서 확인할 수 있어요. 다만 nuxt 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>

API

Props

/**
 * Props for the Error component
 */
interface ErrorProps {
  /**
   * The element or component this component should render as.
   * @default 'main'
   */
  as?: any;
  /**
   * The icon displayed above the status code.
   */
  icon?: any;
  error?: Partial<NuxtError<unknown> & { message: string; }> | undefined;
  /**
   * The URL to redirect to when the error is cleared.
   * @default '/'
   */
  redirect?: string | undefined;
  /**
   * Display a button to clear the error in the links slot.
   * `{ size: 'lg', color: 'primary', variant: 'solid', label: 'Back to home' }`{lang="ts-type"}
   * @default true
   */
  clear?: boolean | ButtonProps | undefined;
  ui?: { root?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; statusCode?: SlotClass; statusMessage?: SlotClass; message?: SlotClass; links?: SlotClass; } | undefined;
}

Slots

/**
 * Slots for the Error component
 */
interface ErrorSlots {
  default(): any;
  leading(): any;
  statusCode(): any;
  statusMessage(): any;
  message(): any;
  links(): any;
}

테마 (Theme)

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'
      }
    }
  }
})

변경 이력 (Changelog)

컴포넌트와 테마의 커밋 히스토리를 참고하세요.


더 알아보기 (Learn more)