悬浮出现在页面角落,显示全局的通知提醒消息。
基础用法
- Element Plus 注册了 $notify 方法并且它接受一个 Object 作为其参数。
- 在最简单的情况下,你可以通过设置 title 和 message 属性来设置通知的标题和正文内容。
- 默认情况下,通知在4500毫秒后自动关闭,但你可以通过设置 duration 属性来自定义通知的展示时间。
- 如果你将它设置为 0,那么通知将不会自动关闭。 需要注意的是 duration 接收一个 Number,单位为毫秒。
<template>
<el-button plain @click="open1"> Closes automatically </el-button>
<el-button plain @click="open2"> Won't close automatically </el-button>
</template>
<script lang="ts" setup>
import { h } from 'vue'
import { ElNotification } from 'element-plus'
const open1 = () => {
ElNotification({
title: 'Title',
message: h('i', { style: 'color: teal' }, 'This is a reminder'),
})
}
const open2 = () => {
ElNotification({
title: 'Prompt',
message: 'This is a message that does not automatically close',
duration: 0,
})
}
</script>
不同类型的通知
- 我们提供了四种不同类型的提醒框:success、warning、info 和error。
- Element Plus 为 Notification 组件准备了5种通知类型:primary,success, warning, info, error。
- 他们可以设置 type 字段来修改,除上述的四个值之外的值会被忽略。 同时,我们也为 Notification 的各种 type 注册了单独的方法,可以在不传入 type 字段的情况下像 open3 和 open4 那样直接调用。
<template>
<el-button plain @click="open5">Primary</el-button>
<el-button plain @click="open1">Success</el-button>
<el-button plain @click="open2">Warning</el-button>
<el-button plain @click="open3">Info</el-button>
<el-button plain @click="open4">Error</el-button>
</template>
<script lang="ts" setup>
import { ElNotification } from 'element-plus'
const open1 = () => {
ElNotification({
title: 'Success',
message: 'This is a success message',
type: 'success',
})
}
const open2 = () => {
ElNotification({
title: 'Warning',
message: 'This is a warning message',
type: 'warning',
})
}
const open3 = () => {
ElNotification({
title: 'Info',
message: 'This is an info message',
type: 'info',
})
}
const open4 = () => {
ElNotification({
title: 'Error',
message: 'This is an error message',
type: 'error',
})
}
const open5 = () => {
ElNotification({
title: 'Primary',
message: 'This is a primary message',
type: 'primary',
})
}
</script>
自定义消息弹出的位置
- 可以让 Notification 从屏幕四角中的任意一角弹出
- 使用 position 属性设置 Notification 的弹出位置, 支持四个选项:top-right、top-left、bottom-right 和 bottom-left, 默认为 top-right。
<template>
<el-button plain @click="open1"> Top Right </el-button>
<el-button plain @click="open2"> Bottom Right </el-button>
<el-button plain @click="open3"> Bottom Left </el-button>
<el-button plain @click="open4"> Top Left </el-button>
</template>
<script lang="ts" setup>
import { ElNotification } from 'element-plus'
const open1 = () => {
ElNotification({
title: 'Custom Position',
message: "I'm at the top right corner",
})
}
const open2 = () => {
ElNotification({
title: 'Custom Position',
message: "I'm at the bottom right corner",
position: 'bottom-right',
})
}
const open3 = () => {
ElNotification({
title: 'Custom Position',
message: "I'm at the bottom left corner",
position: 'bottom-left',
})
}
const open4 = () => {
ElNotification({
title: 'Custom Position',
message: "I'm at the top left corner",
position: 'top-left',
})
}
</script>
有位置偏移的通知栏
- 能够设置偏移量来使 Notification 偏移默认位置。
- Notification 提供设置偏移量的功能,通过设置 offset 字段,可以使弹出的消息距屏幕边缘偏移一段距离。
- 注意在同一时刻,每一个的 Notification 实例应当具有一个相同的偏移量。
<template>
<el-button plain @click="open"> Notification with offset </el-button>
</template>
<script lang="ts" setup>
import { ElNotification } from 'element-plus'
const open = () => {
ElNotification.success({
title: 'Success',
message: 'This is a success message',
offset: 100,
})
}
</script>
使用 HTML 片段作为正文内容
<template>
<el-button plain @click="open"> Use HTML String </el-button>
</template>
<script lang="ts" setup>
import { ElNotification } from 'element-plus'
const open = () => {
ElNotification({
title: 'HTML String',
dangerouslyUseHTMLString: true,
message: '<strong>This is <i>HTML</i> string</strong>',
})
}
</script>
函数形式的 message
<template>
<el-button plain @click="open">Common VNode</el-button>
<el-button plain @click="open1">Dynamic props</el-button>
</template>
<script lang="ts" setup>
import { h, ref } from 'vue'
import { ElNotification, ElSwitch } from 'element-plus'
const open = () => {
ElNotification({
title: 'Use Vnode',
message: h('p', null, [
h('span', null, 'Message can be '),
h('i', { style: 'color: teal' }, 'VNode'),
]),
})
}
const open1 = () => {
const checked = ref<boolean | string | number>(false)
ElNotification({
title: 'Use Vnode',
// Should pass a function if VNode contains dynamic props
message: () =>
h(ElSwitch, {
modelValue: checked.value,
'onUpdate:modelValue': (val: boolean | string | number) => {
checked.value = val
},
}),
})
}
</script>
隐藏关闭按钮
- 通知的关闭按钮可以被设置为隐藏。
- 将 showClose 属性设置为 false 即可隐藏关闭按钮。
<template>
<el-button plain @click="open"> Hide close button </el-button>
</template>
<script lang="ts" setup>
import { ElNotification } from 'element-plus'
const open = () => {
ElNotification.success({
title: 'Info',
message: 'This is a message without close button',
showClose: false,
})
}
</script>









网友评论