• 通知提醒框
    • 何时使用
    • 代码演示
      • 基本
      • 自定义图标
      • 自定义样式
      • 自动关闭的延时
      • 位置
      • 自定义按钮
      • 带有图标的通知提醒框
      • 更新消息内容
  • 何时使用
  • API

    通知提醒框

    全局展示通知提醒信息。

    何时使用

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

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

    代码演示

    Notification通知提醒框 - 图1

    基本

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

    1. <template>
    2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
    3. </template>
    4. <script>
    5. export default {
    6. methods: {
    7. openNotification () {
    8. this.$notification.open({
    9. message: 'Notification Title',
    10. description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
    11. onClick: () => {
    12. console.log('Notification Clicked!');
    13. },
    14. });
    15. },
    16. }
    17. }
    18. </script>

    Notification通知提醒框 - 图2

    自定义图标

    图标可以被自定义。

    1. <template>
    2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
    3. </template>
    4. <script>
    5. export default {
    6. methods: {
    7. openNotification () {
    8. this.$notification.open({
    9. message: 'Notification Title',
    10. description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
    11. icon: <a-icon type="smile" style="color: #108ee9" />,
    12. });
    13. },
    14. }
    15. }
    16. </script>

    Notification通知提醒框 - 图3

    自定义样式

    使用 style 和 className 来定义样式。

    1. <template>
    2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
    3. </template>
    4. <script>
    5. export default {
    6. methods: {
    7. openNotification () {
    8. this.$notification.open({
    9. message: 'Notification Title',
    10. description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
    11. style: {
    12. width: '600px',
    13. marginLeft: `${335 - 600}px`,
    14. },
    15. });
    16. },
    17. }
    18. }
    19. </script>

    Notification通知提醒框 - 图4

    自动关闭的延时

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

    1. <template>
    2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
    3. </template>
    4. <script>
    5. export default {
    6. methods: {
    7. openNotification () {
    8. this.$notification.open({
    9. message: 'Notification Title',
    10. description: 'I will never close automatically. I will be close automatically. I will never close automatically.',
    11. duration: 0,
    12. });
    13. },
    14. }
    15. }
    16. </script>

    Notification通知提醒框 - 图5

    位置

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

    1. <template>
    2. <div>
    3. <a-select v-model="selected" :style="{ width: '120px', marginRight: '10px' }">
    4. <a-select-option v-for="val in options" :key="val" :value="val">{{val}}</a-select-option>
    5. </a-select>
    6. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
    7. </div>
    8. </template>
    9. <script>
    10. const options = ['topLeft', 'topRight', 'bottomLeft', 'bottomRight'];
    11. export default {
    12. data () {
    13. return {
    14. options,
    15. selected: 'topRight',
    16. }
    17. },
    18. watch: {
    19. selected(val) {
    20. this.$notification.config({
    21. placement: val,
    22. });
    23. }
    24. },
    25. methods: {
    26. openNotification (val) {
    27. this.$notification.open({
    28. message: 'Notification Title',
    29. description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
    30. });
    31. },
    32. }
    33. }
    34. </script>

    Notification通知提醒框 - 图6

    自定义按钮

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

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

    Notification通知提醒框 - 图7

    带有图标的通知提醒框

    通知提醒框左侧有图标。

    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>
    10. export default {
    11. methods: {
    12. openNotificationWithIcon (type) {
    13. this.$notification[type]({
    14. message: 'Notification Title',
    15. description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
    16. });
    17. },
    18. }
    19. }
    20. </script>

    Notification通知提醒框 - 图8

    更新消息内容

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

    1. <template>
    2. <a-button type="primary" @click="openNotification">Open the notification box</a-button>
    3. </template>
    4. <script>
    5. const key = 'updatable'
    6. export default {
    7. methods: {
    8. openNotification () {
    9. this.$notification.open({
    10. key,
    11. message: 'Notification Title',
    12. description: 'description.',
    13. });
    14. setTimeout(() => {
    15. this.$notification.open({
    16. key,
    17. message: 'New Title',
    18. description: 'New description.',
    19. });
    20. }, 1000);
    21. },
    22. }
    23. }
    24. </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自定义关闭按钮vueNode |function(h)-
    class自定义 CSS classstring-
    description通知提醒内容,必选string |vueNode |function(h)-
    duration默认 4.5 秒后自动关闭,配置为 null 则不自动关闭number4.5
    icon自定义图标vueNode |function(h)-
    key当前通知唯一标志string-
    message通知提醒标题,必选string |vueNode |function(h)-
    placement弹出位置,可选 topLeft topRight bottomLeft bottomRightstringtopRight
    style自定义内联样式Object | string-
    onClose点击默认关闭按钮时触发的回调函数Function-
    onClick点击通知时触发的回调函数Function-

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

    • 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