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

Message

원문 보기 위키 갱신

Message (메시지)

브라우저 상단(항상 top)에서 내려오는 신탁(oracle) 같은 컴포넌트예요.

  • useMessage로 메시지를 사용하려면, 해당 메서드를 호출하는 컴포넌트를 n-message-provider 안에 넣고 useMessage로 API를 가져와야 해요.
  • setup 밖에서 사용하는 방법이 궁금하다면 이 페이지 하단의 Q & A를 참고하세요.
  • For example:

    <!-- App.vue -->
    <n-message-provider>
      <content />
    </n-message-provider>
    
    import { useMessage } from 'naive-ui'
    import { defineComponent } from 'vue'
    
    // content
    export default defineComponent({
      setup() {
        const message = useMessage()
        return {
          warning() {
            message.warning('...')
          }
        }
      }
    })
    

    출처: 문서

    본문

    Demos

    basic.vue
    icon.vue
    timing.vue
    closable.vue
    modify-content.vue
    manually-close.vue
    about-theme.vue
    multiple-line.vue
    placement.vue
    customize-message.vue
    no-icon.vue
    

    API

    MessageProvider Props

    이름 타입 기본값 설명 버전
    closable boolean false 모든 메시지에 닫기 아이콘을 표시할지 여부예요.
    container-class string undefined 메시지 컨테이너의 클래스예요. 2.36.0
    container-style string | CSSProperties undefined 메시지 컨테이너의 스타일이에요.
    duration number 3000 모든 메시지의 기본 표시 시간이에요.
    keep-alive-on-hover boolean false 모든 메시지 위에 마우스를 올려두면 사라지지 않게 할지 여부예요.
    max number undefined 표시할 메시지의 개수를 제한해요.
    placement top | top-left | top-right | bottom | bottom-left | bottom-right top 모든 메시지의 위치예요.
    to string | HTMLElement 'body' 메시지 컨테이너의 컨테이너 노드예요.

    MessageProvider Injection API

    MessageProvider Injection Methods

    이름 타입 설명 버전
    destroyAll () => void 표시 중인 모든 메시지를 제거해요.
    create (content: string | (() => VNodeChild), option?: MessageOption) => MessageReactive create 타입 메시지를 사용해요. 2.25.7
    error (content: string | (() => VNodeChild), option?: MessageOption) => MessageReactive error 타입 메시지를 사용해요.
    info (content: string | (() => VNodeChild), option?: MessageOption) => MessageReactive info 타입 메시지를 사용해요.
    loading (content: string | (() => VNodeChild), option?: MessageOption) => MessageReactive loading 타입 메시지를 사용해요.
    success (content: string | (() => VNodeChild), option?: MessageOption) => MessageReactive success 타입 메시지를 사용해요.
    warning (content: string | (() => VNodeChild), option?: MessageOption) => MessageReactive warning 타입 메시지를 사용해요.

    MessageOption Properties

    이름 타입 설명 버전
    closable boolean 닫기 아이콘을 표시할지 여부예요.
    duration number 메시지의 표시 시간이에요.
    icon () => VNodeChild 메시지 아이콘이에요.
    keepAliveOnHover boolean 마우스를 올리면 사라지지 않게 할지 여부예요.
    render MessageRenderMessage 메시지 전체를 렌더링하는 함수예요. 2.24.0
    showIcon boolean 아이콘을 표시할지 여부예요. 2.25.7
    spinProps { strokeWidth?: number, stroke?: string, scale?: number, radius?: number } 로딩 아이콘 속성이에요. 2.44.0
    type 'info' | 'success' | 'warning' | 'error' | 'loading' | 'default' 메시지 타입이에요. 'default' 2.25.7
    onAfterLeave () => void 메시지가 사라진 뒤에 호출되는 콜백이에요.
    onClose () => void 닫기 아이콘을 클릭했을 때 호출되는 콜백이에요.
    onLeave () => void 메시지가 사라지기 시작할 때 호출되는 콜백이에요.

    MessageRenderMessage Type

    type MessageRenderMessage = (props: {
      content?: string | number | (() => VNodeChild)
      icon?: () => VNodeChild
      closable: boolean
      type: 'info' | 'success' | 'warning' | 'error' | 'loading'
      onClose?: () => void
    }) => VNodeChild
    

    MessageReactive Properties

    이름 타입 설명 버전
    closable boolean 닫기 아이콘을 표시할지 여부예요.
    content string | (() => VNodeChild) 메시지 콘텐츠예요.
    destroy () => void 메시지 제거 메서드예요.
    icon () => VNodeChild 메시지 아이콘이에요.
    keepAliveOnHover boolean 마우스를 올리면 사라지지 않게 할지 여부예요.
    showIcon boolean 아이콘을 표시할지 여부예요. 2.25.7
    type 'info' | 'success' | 'warning' | 'error' | 'loading' | 'default' 메시지 타입이에요. 'default' 2.25.7
    onAfterLeave () => void 메시지가 사라진 뒤에 호출되는 콜백이에요.
    onLeave () => void 메시지가 사라지기 시작할 때 호출되는 콜백이에요.

    MessageReactive Methods

    이름 타입 설명
    destroy () 메시지 제거 메서드예요.

    Q & A

    Use Message Outside Setup

    Option 1

    createDiscreteApi를 사용해요. 이 방법을 쓰려면 주의사항(caveat)을 꼼꼼히 읽어보세요. 같은 앱에서 useMessage와 함께 쓰지 않는 게 좋아요.

    Option 2

    useMessage의 반환 값을 최상위 setup에서 window에 붙인 다음 호출해야 해요. 호출하기 전에 메시지가 성공적으로 마운트되었는지 먼저 확인해야 하죠.
    <!-- App.vue -->
    <n-message-provider>
      <content />
    </n-message-provider>
    
    <!-- content.vue -->
    <template>...</template>
    
    <script>
      import { useMessage } from 'naive-ui'
      import { defineComponent } from 'vue'
    
      // content
      export default defineComponent({
        setup() {
          window.$message = useMessage()
        }
      })
    </script>
    
    // xxx.js
    export function handler() {
      // You need to ensure that window.$message = message has been executed in setup
      window.$message.success(
        'Cause you walked hand in hand With another man in my place'
      )
    }
    

    더 알아보기 (Learn more)

    컴포넌트 트리 바깥에서 팝업류 API를 쓰는 방법이 궁금하다면 Discrete API 문서를 참고하세요. 알림이 필요하다면 Notification 컴포넌트와의 차이도 비교해 보세요.