Skip to content

Notification 通知

悬浮出现在页面角落,显示全局的通知提醒消息。

基础用法

从右上角出现,4.5 秒后自动消失。

vue
<template>
  <x-button @click="handleBasic">基本用法</x-button>
</template>

<script setup>
import { Notification } from '@xto/feedback'

const handleBasic = () => {
  Notification({
    title: '标题名称',
    message: '这是一条通知消息'
  })
}
</script>

不同类型

Notification 支持四种类型:success、warning、info、error。

vue
<script setup>
import { Notification } from '@xto/feedback'

const handleSuccess = () => {
  Notification.success({
    title: '成功',
    message: '这是一条成功的提示消息'
  })
}

const handleWarning = () => {
  Notification.warning({
    title: '警告',
    message: '这是一条警告的提示消息'
  })
}
</script>

自定义弹出位置

Notification 支持 four 种弹出位置:top-righttop-leftbottom-rightbottom-left

vue
<script setup>
import { Notification } from '@xto/feedback'

const handlePosition = (position) => {
  Notification({
    title: '自定义位置',
    message: `这是一条${position}的通知`,
    position
  })
}
</script>

隐藏关闭按钮

可以不显示关闭按钮。

vue
<script setup>
import { Notification } from '@xto/feedback'

const handleHiddenClose = () => {
  Notification({
    title: '隐藏关闭按钮',
    message: '这是一条没有关闭按钮的消息',
    showClose: false
  })
}
</script>

不会自动关闭

将 duration 设置为 0 不会自动关闭。

vue
<script setup>
import { Notification } from '@xto/feedback'

const handleNoClose = () => {
  Notification({
    title: '不会自动关闭',
    message: '这是一条不会自动关闭的消息,点击关闭按钮才能关闭',
    duration: 0
  })
}
</script>

使用 HTML 片段

message 属性支持传入 HTML 片段。

vue
<script setup>
import { Notification } from '@xto/feedback'

const handleHTML = () => {
  Notification({
    title: 'HTML 片段',
    dangerouslyUseHTMLString: true,
    message: '<strong>这是 <i>HTML</i> 片段</strong>'
  })
}
</script>

API

方法

方法名说明参数
Notification显示通知NotificationOptions | string
Notification.success显示成功通知NotificationOptions | string
Notification.warning显示警告通知NotificationOptions | string
Notification.info显示信息通知NotificationOptions | string
Notification.error显示错误通知NotificationOptions | string
Notification.closeAll关闭所有通知

NotificationOptions

属性说明类型默认值
title标题string
message内容string
type通知类型'success' | 'warning' | 'info' | 'error'
duration显示时间,毫秒。设为 0 则不会自动关闭number4500
position自定义弹出位置'top-right' | 'top-left' | 'bottom-right' | 'bottom-left''top-right'
showClose是否显示关闭按钮booleantrue
offset偏移距离number0
customClass自定义类名string
dangerouslyUseHTMLString是否将 message 作为 HTML 渲染booleanfalse
onClose关闭时的回调函数() => void

返回值

Notification 方法返回一个 NotificationInstance 对象:

属性说明类型
close关闭当前通知() => void

基于 MIT 许可发布