App
App는 몇 가지 전역적인 사용을 위한 애플리케이션 래퍼 컴포넌트예요.
출처: 문서
본문
언제 사용하나요
.ant-app요소를 기반으로 리셋 스타일을 제공해요.useApp을 통해message/notification/Modal의 정적 메서드를contextHolder를 직접 작성하지 않고도 사용할 수 있어요.
API
App
| 속성 | 설명 | 타입 | 기본값 | 버전 |
|---|---|---|---|---|
| message | Message 전역 설정 | MessageConfig | - | 4.x |
| notification | Notification 전역 설정 | NotificationConfig | - | 4.x |
사용 방법
기본 사용법
App은 provide/inject를 통해 상위·하위 메서드 호출을 제공해요. useApp은 하위 컴포넌트로 사용해야 하기 때문에, 애플리케이션의 최상위 레벨에 App을 감싸는 걸 권장해요.
/*myPage.vue*/
<template>
<a-space>
<a-button type="primary" @click="showMessage">Open message</a-button>
<a-button type="primary" @click="showModal">Open modal</a-button>
<a-button type="primary" @click="showNotification">Open notification</a-button>
</a-space>
</template>
<script setup lang="ts">
import { App } from 'ant-design-vue';
const { message, modal, notification } = App.useApp();
const showMessage = () => {
message.success('Success!');
};
const showModal = () => {
modal.warning({
title: 'This is a warning message',
content: 'some messages...some messages...',
});
};
const showNotification = () => {
notification.info({
message: `Notification topLeft`,
description: 'Hello, Ant Design Vue!!',
placement: 'topLeft',
});
};
</script>
참고: App.useApp은 App 하위에서만 사용할 수 있어요.
중첩 사용 시나리오 (꼭 필요한 경우가 아니면 중첩은 피하는 게 좋아요)
<a-app>
<a-space>
...
<a-app>...</a-app>
</a-space>
</a-app>
ConfigProvider와의 조합
App 컴포넌트는 ConfigProvider 안의 토큰만 사용할 수 있어요. 토큰을 사용해야 한다면 ConfigProvider와 App 컴포넌트는 반드시 쌍으로 등장해야 해요.
<a-config-provider theme="{{ ... }}">
<a-app>...</a-app>
</a-config-provider>
전역 시나리오 (pinia 시나리오)
import { App } from 'ant-design-vue';
import type { MessageInstance } from 'ant-design-vue/es/message/interface';
import type { ModalStaticFunctions } from 'ant-design-vue/es/modal/confirm';
import type { NotificationInstance } from 'ant-design-vue/es/notification/interface';
export const useGlobalStore = defineStore('global', () => {
const message: MessageInstance = ref();
const notification: NotificationInstance = ref();
const modal: Omit<ModalStaticFunctions, 'warn'> = ref();
(() => {
const staticFunction = App.useApp();
message.value = staticFunction.message;
modal.value = staticFunction.modal;
notification.value = staticFunction.notification;
})();
return { message, notification, modal };
});
// sub page
<template>
<a-space>
<a-button type="primary" @click="showMessage">Open message</a-button>
</a-space>
</template>
<script setup>
import { useGlobalStore } from '@/stores/global';
const global = useGlobalStore();
const showMessage = () => {
global.message.success('Success!');
};
</script>