Notification通知提醒框 - 图1

Notification 通知提醒框

全局展示通知提醒信息。

何时使用

在系统四个角显示通知提醒信息。经常用于以下情况:

  • 较为复杂的通知内容。
  • 带有交互的通知,给出用户下一步的行动点。
  • 系统主动推送。

代码演示

Open the notification box

基本用法

最简单的用法,4.5 秒后自动关闭。

  1. <template>
  2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
  3. </template>
  4. <script lang="ts">
  5. import { notification } from 'ant-design-vue';
  6. import { defineComponent } from 'vue';
  7. export default defineComponent({
  8. setup() {
  9. const openNotification = () => {
  10. notification.open({
  11. message: 'Notification Title',
  12. description:
  13. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
  14. onClick: () => {
  15. console.log('Notification Clicked!');
  16. },
  17. });
  18. };
  19. return {
  20. openNotification,
  21. };
  22. },
  23. });
  24. </script>

Notification通知提醒框 - 图2

带有图标的通知提醒框

通知提醒框左侧有图标。

  1. <template>
  2. <div>
  3. <a-button @click="() => openNotificationWithIcon('success')">Success</a-button>
  4. <a-button @click="() => openNotificationWithIcon('info')">Info</a-button>
  5. <a-button @click="() => openNotificationWithIcon('warning')">Warning</a-button>
  6. <a-button @click="() => openNotificationWithIcon('error')">Error</a-button>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { notification } from 'ant-design-vue';
  11. import { defineComponent } from 'vue';
  12. export default defineComponent({
  13. setup() {
  14. const openNotificationWithIcon = (type: string) => {
  15. notification[type]({
  16. message: 'Notification Title',
  17. description:
  18. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
  19. });
  20. };
  21. return {
  22. openNotificationWithIcon,
  23. };
  24. },
  25. });
  26. </script>

Open the notification box

自定义按钮

自定义关闭按钮的样式和文字。

  1. <template>
  2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
  3. </template>
  4. <script lang="ts">
  5. import { notification, Button } from 'ant-design-vue';
  6. import { h, defineComponent } from 'vue';
  7. const close = () => {
  8. console.log(
  9. 'Notification was closed. Either the close button was clicked or duration time elapsed.',
  10. );
  11. };
  12. export default defineComponent({
  13. setup() {
  14. const openNotification = () => {
  15. const key = `open${Date.now()}`;
  16. notification.open({
  17. message: 'Notification Title',
  18. description:
  19. 'A function will be be called after the notification is closed (automatically after the "duration" time of manually).',
  20. btn: h(
  21. Button,
  22. {
  23. type: 'primary',
  24. size: 'small',
  25. onClick: () => notification.close(key),
  26. },
  27. 'Confirm',
  28. ),
  29. key,
  30. onClose: close,
  31. });
  32. };
  33. return {
  34. openNotification,
  35. };
  36. },
  37. });
  38. </script>

Notification通知提醒框 - 图3

位置

可以设置通知从右上角、右下角、左下角、左上角弹出。

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="openNotification('topLeft')">
  4. <radius-upleft-outlined />
  5. topLeft
  6. </a-button>
  7. <a-button type="primary" @click="openNotification('topRight')">
  8. <radius-upright-outlined />
  9. topRight
  10. </a-button>
  11. <a-divider />
  12. <a-button type="primary" @click="openNotification('bottomLeft')">
  13. <radius-bottomleft-outlined />
  14. bottomLeft
  15. </a-button>
  16. <a-button type="primary" @click="openNotification('bottomRight')">
  17. <radius-bottomright-outlined />
  18. bottomRight
  19. </a-button>
  20. </div>
  21. </template>
  22. <script lang="ts">
  23. import {
  24. RadiusUpleftOutlined,
  25. RadiusUprightOutlined,
  26. RadiusBottomleftOutlined,
  27. RadiusBottomrightOutlined,
  28. } from '@ant-design/icons-vue';
  29. import { notification } from 'ant-design-vue';
  30. import { defineComponent } from 'vue';
  31. export default defineComponent({
  32. components: {
  33. RadiusUpleftOutlined,
  34. RadiusUprightOutlined,
  35. RadiusBottomleftOutlined,
  36. RadiusBottomrightOutlined,
  37. },
  38. setup() {
  39. const openNotification = (placement: string) => {
  40. notification.open({
  41. message: `Notification ${placement}`,
  42. description:
  43. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
  44. placement,
  45. });
  46. };
  47. return {
  48. openNotification,
  49. };
  50. },
  51. });
  52. </script>

Open the notification box

自动关闭的延时

自定义通知框自动关闭的延时,默认 4.5s,取消自动关闭只要将该值设为 0 即可。

  1. <template>
  2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
  3. </template>
  4. <script lang="ts">
  5. import { notification } from 'ant-design-vue';
  6. import { defineComponent } from 'vue';
  7. export default defineComponent({
  8. setup() {
  9. const openNotification = () => {
  10. notification.open({
  11. message: 'Notification Title',
  12. description:
  13. 'I will never close automatically. I will be close automatically. I will never close automatically.',
  14. duration: 0,
  15. });
  16. };
  17. return {
  18. openNotification,
  19. };
  20. },
  21. });
  22. </script>

Open the notification box

自定义图标

图标可以被自定义。

  1. <template>
  2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
  3. </template>
  4. <script lang="ts">
  5. import { SmileOutlined } from '@ant-design/icons-vue';
  6. import { notification } from 'ant-design-vue';
  7. import { defineComponent, h } from 'vue';
  8. export default defineComponent({
  9. setup() {
  10. const openNotification = () => {
  11. notification.open({
  12. message: 'Notification Title',
  13. description:
  14. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
  15. icon: h(SmileOutlined, { style: 'color: #108ee9' }),
  16. });
  17. };
  18. return {
  19. openNotification,
  20. };
  21. },
  22. });
  23. </script>

Open the notification box

自定义样式

使用 style 和 className 来定义样式。

  1. <template>
  2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
  3. </template>
  4. <script>
  5. import { notification } from 'ant-design-vue';
  6. import { defineComponent } from 'vue';
  7. export default defineComponent({
  8. setup() {
  9. const openNotification = () => {
  10. notification.open({
  11. message: 'Notification Title',
  12. description:
  13. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
  14. style: {
  15. width: '600px',
  16. marginLeft: `${335 - 600}px`,
  17. },
  18. });
  19. };
  20. return {
  21. openNotification,
  22. };
  23. },
  24. });
  25. </script>

Open the notification box

更新消息内容

可以通过唯一的 key 来更新内容。

  1. <template>
  2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
  3. </template>
  4. <script lang="ts">
  5. import { notification } from 'ant-design-vue';
  6. import { defineComponent } from 'vue';
  7. const key = 'updatable';
  8. export default defineComponent({
  9. setup() {
  10. const openNotification = () => {
  11. notification.open({
  12. key,
  13. message: 'Notification Title',
  14. description: 'description.',
  15. });
  16. setTimeout(() => {
  17. notification.open({
  18. key,
  19. message: 'New Title',
  20. description: 'New description.',
  21. });
  22. }, 1000);
  23. };
  24. return {
  25. openNotification,
  26. };
  27. },
  28. });
  29. </script>

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

config 参数如下:

参数说明类型默认值版本
btn自定义关闭按钮VNode-
bottom消息从底部弹出时,距离底部的位置,单位像素。string24px
class自定义 CSS classstring-
description通知提醒内容,必选string |VNode-
duration默认 4.5 秒后自动关闭,配置为 null 则不自动关闭number4.5
getContainer配置渲染节点的输出位置() => HTMLNode() => document.body
icon自定义图标VNode-
key当前通知唯一标志string-
message通知提醒标题,必选string |VNode-
placement弹出位置,可选 topLeft topRight bottomLeft bottomRightstringtopRight
style自定义内联样式Object | string-
onClose点击默认关闭按钮时触发的回调函数Function-
onClick点击通知时触发的回调函数Function-
top消息从顶部弹出时,距离顶部的位置,单位像素。string24px
closeIcon自定义关闭图标VNode-

还提供了一个全局配置方法,在调用前提前配置,全局一次生效。

  • notification.config(options)
  1. notification.config({
  2. placement: 'bottomRight',
  3. bottom: '50px',
  4. duration: 3,
  5. });
参数说明类型默认值版本
bottom消息从底部弹出时,距离底部的位置,单位像素。string24px
duration默认自动关闭延时,单位秒number4.5
getContainer配置渲染节点的输出位置() => HTMLNode() => document.body
placement弹出位置,可选 topLeft topRight bottomLeft bottomRightstringtopRight
top消息从顶部弹出时,距离顶部的位置,单位像素。string24px
closeIcon自定义关闭图标VNode-