Skip to content

Notification 通知

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

基础用法

注册了一个 createNotification 方法用于调用。 你可以通过设置 titlemessage 属性来设置通知的标题和正文内容。默认情况下,通知在 3000 毫秒后自动关闭,但你可以通过设置 duration 属性来自定义通知的展示时间。 如果你将它设置为 0,那么通知将不会自动关闭。 需要注意的是 duration接收一个Number,单位为毫秒。

<script setup>
import { h } from 'vue'
import { createNotification } from '@com/Notification/method'
import Button from '@com/Button/Button.vue'
const open = () => {
  createNotification({ title: 'this is the title', message: 'hello world' })
}
const open2 = () => {
  createNotification({ title: 'this is the title', message: h('b', 'this is bold') })
}
</script>
<template>
  <div class="basic block">
    <Button type="primary" @click="open"> 创建一条通知 </Button>
    <Button @click="open2"> 创建支持 VNode 的通知 </Button>
  </div>
</template>

不同类型的通知

用来显示「成功、警告、消息、错误」类的操作反馈。设置 type 字段可以定义不同的状态,默认为info

<script setup>
import { createNotification } from '@com/Notification/method'
import Button from '@com/Button/Button.vue'
const open = (type) => {
  createNotification({ title: 'this is the title', message: 'hello world', type })
}
</script>
<template>
  <div class="basic block">
    <Button @click="open('success')" type="success"> Success </Button>
    <Button @click="open('info')" type="info"> Info </Button>
    <Button @click="open('warning')" type="warning"> warning </Button>
    <Button @click="open('danger')" type="danger"> Danger </Button>
  </div>
</template>

自定义图标类型

可以使用 iconName 属性来自定义通知左侧图标。图标名称请看 fontawesome 官网 https://fontawesome.com/icons

<script setup>
import { createNotification } from '@com/Notification/method'
import Button from '@com/Button/Button.vue'
const open = () => {
  createNotification({ title: 'this is the title', message: 'hello world', icon: 'star' })
}
</script>
<template>
  <div class="basic block">
    <Button type="primary" @click="open"> 创建一条带图标的通知 </Button>
  </div>
</template>

APIs

属性

使用 createNotification 创建通知,它接受一个Object,以下是 Object 中的属性列表。

属性名称描述类型默认值
title通知标题string''
message通知文字string | vNode
type通知类型success| warning | info | dangerinfo
showClose是否显示关闭按钮booleanfalse
duration显示时间,单位为毫秒。 设为 0 则不会自动关闭number3000
icon左侧自定义图标名称string

方法

调用 createNotification 会返回当前 Notification 的实例。 如果需要手动关闭实例,可以调用它的 close 方法。

名称描述类型
close关闭当前的 通知() => void