|
| 1 | +--- |
| 2 | +category: Components |
| 3 | +cols: 1 |
| 4 | +type: Other |
| 5 | +title: App |
| 6 | +cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*TBTSR4PyVmkAAAAAAAAAAAAADrJ8AQ/original |
| 7 | +coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*JGb3RIzyOCkAAAAAAAAAAAAADrJ8AQ/original |
| 8 | +--- |
| 9 | + |
| 10 | +Application wrapper for some global usages. |
| 11 | + |
| 12 | +## When To Use |
| 13 | + |
| 14 | +- Provide reset styles based on `.ant-app` element. |
| 15 | +- You could use static methods of `message/notification/Modal` form `useApp` without writing `contextHolder` manually. |
| 16 | + |
| 17 | +## API |
| 18 | + |
| 19 | +### App |
| 20 | + |
| 21 | +| Property | Description | Type | Default | Version | |
| 22 | +| --- | --- | --- | --- | --- | |
| 23 | +| message | Global config for Message | [MessageConfig](/components/message/#messageconfig) | - | 4.x | |
| 24 | +| notification | Global config for Notification | [NotificationConfig](/components/notification/#notificationconfig) | - | 4.x | |
| 25 | + |
| 26 | +## How to use |
| 27 | + |
| 28 | +### Basic usage |
| 29 | + |
| 30 | +App provides upstream and downstream method calls through `provide/inject`, because useApp needs to be used as a subcomponent, we recommend encapsulating App at the top level in the application. |
| 31 | + |
| 32 | +```html |
| 33 | +/*myPage.vue*/ |
| 34 | +<template> |
| 35 | + <a-space> |
| 36 | + <a-button type="primary" @click="showMessage">Open message</a-button> |
| 37 | + <a-button type="primary" @click="showModal">Open modal</a-button> |
| 38 | + <a-button type="primary" @click="showNotification">Open notification</a-button> |
| 39 | + </a-space> |
| 40 | +</template> |
| 41 | + |
| 42 | +<script setup lang="ts"> |
| 43 | + import { App } from 'ant-design-vue'; |
| 44 | +
|
| 45 | + const { message, modal, notification } = App.useApp(); |
| 46 | +
|
| 47 | + const showMessage = () => { |
| 48 | + message.success('Success!'); |
| 49 | + }; |
| 50 | +
|
| 51 | + const showModal = () => { |
| 52 | + modal.warning({ |
| 53 | + title: 'This is a warning message', |
| 54 | + content: 'some messages...some messages...', |
| 55 | + }); |
| 56 | + }; |
| 57 | +
|
| 58 | + const showNotification = () => { |
| 59 | + notification.info({ |
| 60 | + message: `Notification topLeft`, |
| 61 | + description: 'Hello, Ant Design Vue!!', |
| 62 | + placement: 'topLeft', |
| 63 | + }); |
| 64 | + }; |
| 65 | +</script> |
| 66 | +``` |
| 67 | + |
| 68 | +Note: App.useApp must be available under App. |
| 69 | + |
| 70 | +#### Embedded usage scenarios (if not necessary, try not to do nesting) |
| 71 | + |
| 72 | +```html |
| 73 | +<a-app> |
| 74 | + <a-space> |
| 75 | + ... |
| 76 | + <a-app>...</a-app> |
| 77 | + </a-space> |
| 78 | +</a-app> |
| 79 | +``` |
| 80 | + |
| 81 | +#### Sequence with ConfigProvider |
| 82 | + |
| 83 | +The App component can only use the token in the `ConfigProvider`, if you need to use the Token, the ConfigProvider and the App component must appear in pairs. |
| 84 | + |
| 85 | +```html |
| 86 | +<a-config-provider theme="{{ ... }}"> |
| 87 | + <a-app>...</a-app> |
| 88 | +</a-config-provider> |
| 89 | +``` |
| 90 | + |
| 91 | +#### Global scene (pinia scene) |
| 92 | + |
| 93 | +```ts |
| 94 | +import { App } from 'ant-design-vue'; |
| 95 | +import type { MessageInstance } from 'ant-design-vue/es/message/interface'; |
| 96 | +import type { ModalStaticFunctions } from 'ant-design-vue/es/modal/confirm'; |
| 97 | +import type { NotificationInstance } from 'ant-design-vue/es/notification/interface'; |
| 98 | + |
| 99 | +export const useGloablStore = defineStore('global', () => { |
| 100 | + const message: MessageInstance = ref(); |
| 101 | + const notification: NotificationInstance = ref(); |
| 102 | + const modal: Omit<ModalStaticFunctions, 'warn'> = ref(); |
| 103 | + (() => { |
| 104 | + const staticFunction = App.useApp(); |
| 105 | + message.value = staticFunction.message; |
| 106 | + modal.value = staticFunction.modal; |
| 107 | + notification.value = staticFunction.notification; |
| 108 | + })(); |
| 109 | + |
| 110 | + return { message, notification, modal }; |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +```html |
| 115 | +// sub page |
| 116 | +<template> |
| 117 | + <a-space> |
| 118 | + <a-button type="primary" @click="showMessage">Open message</a-button> |
| 119 | + </a-space> |
| 120 | +</template> |
| 121 | + |
| 122 | +<script setup> |
| 123 | + import { useGlobalStore } from '@/stores/global'; |
| 124 | + const global = useGlobalStore(); |
| 125 | + const showMessage = () => { |
| 126 | + global.message.success('Success!'); |
| 127 | + }; |
| 128 | +</script> |
| 129 | +``` |
0 commit comments