Element Plus 注册了 $notify 方法并且它接受一个 Object 作为其参数。在最简单的情况下,你可以通过设置 title 和 message 属性来设置通知的标题和正文内容。默认情况下,通知在4500毫秒后自动关闭,但你可以通过设置 duration 属性来自定义通知的展示时间。如果你将它设置为 0,那么通知将不会自动关闭。需要注意的是 duration 接收一个 Number,单位为毫秒。
export function NotificationBasic() { return ( <div className={styles.row}> <ElButton plain onClick={() => notification({ title: "Title", message: <i style={{ color: "teal" }}>This is a reminder</i>, }) } > Closes automatically </ElButton> <ElButton plain onClick={() => notification({ title: "Prompt", message: "This is a message that does not automatically close", duration: 0, }) } > Won't close automatically </ElButton> </div> );}
不同类型的通知
We provide five types: primary, success, warning, info and error.
Element Plus provides five notification types: primary, success, warning, info and error. 他们可以设置 type 字段来修改,除上述的四个值之外的值会被忽略。同时,我们也为 Notification 的各种 type 注册了单独的方法,可以在不传入 type 字段的情况下像 open3 和 open4 那样直接调用。 primary 已被添加到2.9.11。
export function NotificationDifferentTypes() { return ( <div className={styles.row}> <ElButton plain onClick={() => notification({ title: "Primary", message: "This is a primary message", type: "primary", }) } > Primary </ElButton> <ElButton plain onClick={() => notification({ title: "Success", message: "This is a success message", type: "success", }) } > Success </ElButton> <ElButton plain onClick={() => notification({ title: "Warning", message: "This is a warning message", type: "warning", }) } > Warning </ElButton> <ElButton plain onClick={() => notification({ title: "Info", message: "This is a info message", type: "info", }) } > Info </ElButton> <ElButton plain onClick={() => notification({ title: "Error", message: "This is a error message", type: "error", }) } > Error </ElButton> </div> );}
自定义消息弹出的位置
可以让 Notification 从屏幕四角中的任意一角弹出
使用 position 属性设置 Notification 的弹出位置,支持四个选项:top-right、top-left、bottom-right 和 bottom-left,默认为 top-right。
export function NotificationPositioning() { return ( <div className={styles.row}> <ElButton plain onClick={() => notification({ title: "Custom Position", message: "I'm at the top right corner", position: "top-right", }) } > Top Right </ElButton> <ElButton plain onClick={() => notification({ title: "Custom Position", message: "I'm at the bottom right corner", position: "bottom-right", }) } > Bottom Right </ElButton> <ElButton plain onClick={() => notification({ title: "Custom Position", message: "I'm at the bottom left corner", position: "bottom-left", }) } > Bottom Left </ElButton> <ElButton plain onClick={() => notification({ title: "Custom Position", message: "I'm at the top left corner", position: "top-left", }) } > Top Left </ElButton> </div> );}