Element-React Notification 通知

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

基本用法

适用性广泛的通知栏

Notification 组件提供通知功能,Element 注册了Notification方法,接收一个options字面量参数,在最简单的情况下,你可以设置title字段和message字段,用于设置通知的标题和正文。默认情况下,经过一段时间后 Notification 组件会自动关闭,但是通过设置duration,可以控制关闭的时间间隔,特别的是,如果设置为0,则不会自动关闭。注意:duration接收一个Number,单位为毫秒,默认为4500
Element-React Notification 通知

render() {
  return (
    <div>
      <Button plain={true} onClick={this.open.bind(this)}>可自动关闭</Button>
      <Button plain={true} onClick={this.open2.bind(this)}>不会自动关闭</Button>
    </div>
  )
}


open() {
  Notification({
    title: 标题名称,
    message: 这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案这是提示文案
  });
}


open2() {
  Notification({
    title: 提示,
    message: 这是一条不会自动关闭的消息,
    duration: 0
  });
}

带有倾向性

带有 icon,常用来显示「成功、警告、消息、错误」类的系统消息

Element 为 Notification 组件准备了四种通知类型:success, warning, info, error。通过type字段来设置,除此以外的值将被忽略。同时,我们也为 Notification 的各种 type 注册了方法,可以在不传入type字段的情况下像open5open6那样直接调用。
Element-React Notification 通知

render() {
  return (
    <div>
      <Button plain={true} onClick={this.open3.bind(this)}>成功</Button>
      <Button plain={true} onClick={this.open4.bind(this)}>警告</Button>
      <Button plain={true} onClick={this.open5.bind(this)}>消息</Button>
      <Button plain={true} onClick={this.open6.bind(this)}>错误</Button>
    </div>
  )
}


open3() {
  Notification({
    title: 成功,
    message: 这是一条成功的提示消息,
    type: success
  });
}


open4() {
  Notification({
    title: 警告,
    message: 这是一条警告的提示消息,
    type: warning
  });
}


open5() {
  Notification.info({
    title: 消息,
    message: 这是一条消息的提示消息
  });
}


open6() {
  Notification.error({
    title: 错误,
    message: 这是一条错误的提示消息
  });
}

带有偏移

让 Notification 偏移一些位置

Notification 提供设置偏移量的功能,通过设置 offset 字段,可以使弹出的消息距屏幕顶部偏移一段距离。注意在同一时刻,所有的 Notification 实例应当具有一个相同的偏移量。

Element-React Notification 通知

render() {
  return (
    <Button plain={true} onClick={this.open.bind(this)}>偏移的通知</Button>
  )
}


open() {
  Notification({
    title: 成功,
    message: 这是一条成功的提示消息,
    offset: 100
  });
}

单独引用

单独引入 Notification:

import { Notification } from element-react;

此时调用方法为 Notification(options)。我们也为每个 type 定义了各自的方法,如 Notification.success(options)

参数

参数说明类型可选值默认值
title标题string
message说明文字string/ReactElement
type主题样式,如果不在可选值内将被忽略stringsuccess/warning/info/error
iconClass自定义图标的类名。若设置了 type,则 iconClass 会被覆盖string
duration显示时间, 毫秒。设为 0 则不会自动关闭number4500
onClose关闭时的回调函数function
onClick点击 Notification 时的回调函数function
offset偏移的距离,在同一时刻,所有的 Notification 实例应当具有一个相同的偏移量number0

方法

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

方法名说明
close关闭当前的 Notification

作者:冒牌SEO,如若转载,请注明出处:https://www.web176.com/elementreact/19979.html

(0)
打赏 支付宝 支付宝 微信 微信
冒牌SEO冒牌SEO
上一篇 2023年5月11日
下一篇 2023年5月11日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注