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

Notification

원문 보기 위키 갱신

Notification (알림)

전역으로 알림 메시지를 표시하는 컴포넌트예요. 뷰포트의 네 모서리 중 원하는 위치에 알림을 띄울 수 있어요.

출처: 문서

본문

언제 사용하나요

뷰포트의 네 모서리 어디에든 알림 메시지를 표시할 때 사용해요. 주로 다음과 같은 경우에 사용해요:

  • 복잡한 내용의 알림.
  • 사용자 상호작용에 기반한 피드백을 제공하는 알림. 또는 사용자가 따라야 할 앞으로의 단계에 대한 세부 정보를 보여줄 때.
  • 애플리케이션에 의해 푸시되는 알림.

API

  • notification.success(config)
  • notification.error(config)
  • notification.info(config)
  • notification.warning(config)
  • notification.warn(config)
  • notification.open(config)
  • notification.close(key: String)
  • notification.destroy()
  • notification.useNotification()

config의 속성은 다음과 같아요:

속성 설명 타입 기본값 버전
bottom placement가 bottomRight 또는 bottomLeft일 때 뷰포트 바닥에서의 거리(단위: 픽셀). string 24px
btn 커스텀 닫기 버튼 VNode | () => VNode -
class 커스텀 CSS 클래스 string -
closeIcon 커스텀 닫기 아이콘 VNode | () => VNode -
description 알림 상자의 내용(필수) string| VNode | () => VNode -
duration Notification이 닫히기 전 시간(초). 0 또는 null로 설정하면 자동으로 닫히지 않아요 number 4.5
getContainer Notification용 마운트 노드 반환 () => HTMLNode () => document.body
icon 커스텀 아이콘 VNode | () => VNode -
key Notification의 고유 식별자 string -
message 알림 상자의 제목(필수) string| VNode | () => VNode -
placement Notification의 위치, top topLeft topRight bottom bottomLeft bottomRight 중 하나 string topRight top bottom 3.3.0
style 커스텀 인라인 스타일 Object | string -
top placement가 topRight 또는 topLeft일 때 뷰포트 상단에서의 거리(단위: 픽셀). string 24px
onClick 알림을 클릭했을 때 호출되는 함수 지정 Function -
onClose 닫기 버튼을 클릭했을 때 호출되는 함수 지정 Function -

notification은 기본 옵션 지정에 사용할 수 있는 전역 config() 메서드도 제공해요. 이 메서드를 사용하면 표시할 때 모든 알림 상자가 전역으로 정의된 옵션을 반영해요.

  • notification.config(options)

    전역 설정에 ConfigProvider를 사용하면 기본적으로 RTL 모드가 자동으로 시작돼요.(4.3.0+)

    단독으로 사용하려면 다음 설정으로 RTL 모드를 시작할 수 있어요.

notification.config({
  placement: 'bottomRight',
  bottom: '50px',
  duration: 3,
  rtl: true,
});
속성 설명 타입 기본값 버전
bottom placement가 bottomRight 또는 bottomLeft일 때 뷰포트 바닥에서의 거리(단위: 픽셀). string 24px
closeIcon 커스텀 닫기 아이콘 VNode | () => VNode -
duration Notification이 닫히기 전 시간(초). 0 또는 null로 설정하면 자동으로 닫히지 않아요 number 4.5
getContainer Notification용 마운트 노드 반환 () => HTMLNode () => document.body
maxCount 최대 Notification 표시 수, 한도를 초과하면 오래된 것부터 제거 number - 3.0
placement Notification의 위치, topLeft topRight bottomLeft bottomRight 중 하나 string topRight
rtl RTL 모드 활성화 여부 boolean false
top placement가 topRight 또는 topLeft일 때 뷰포트 상단에서의 거리(단위: 픽셀). string 24px

FAQ

notification에서 context, Pinia, ConfigProvider locale/prefixCls/theme에 접근할 수 없는 이유는 뭔가요?

antdv는 notification 메서드를 호출할 때 Vue.render로 Vue 인스턴스를 동적으로 생성해요. 그 컨텍스트는 원래 코드가 위치한 컨텍스트와 달라요.

ConfigProvider 컨텍스트 같은 context 정보가 필요할 때는 notification.useNotification로 api 인스턴스와 contextHolder 노드를 가져와 자식에 넣을 수 있어요:

<template>
  <contextHolder />
  <!-- <component :is='contextHolder'/> -->
</template>
<script setup>
  import { notification } from 'ant-design-vue';
  const [notificationApi, contextHolder] = notification.useNotification();
  notificationApi.open({
    // ...
  });
</script>

참고: hooks가 있는 자식에 contextHolder를 반드시 넣어야 해요. 컨텍스트 연결이 필요 없다면 원래 메서드를 사용하면 돼요.

App 컴포넌트를 사용하면 useNotification와 같이 contextHolder를 수동으로 심어야 하는 메서드들의 문제를 간단히 해결할 수 있어요.

정적 메서드의 prefixCls는 어떻게 설정하나요?

ConfigProvider.config로 설정할 수 있어요.

더 알아보기 (Learn more)