当前位置:首页 > 前端教程

elementui notify

2026-03-05 23:40:40前端教程

使用 ElementUI 的 Notify 通知组件

ElementUI 提供了 Notify 通知组件,用于在页面顶部显示全局通知消息。以下是使用方法和常见配置。

基本用法

通过 this.$notify 调用通知组件,支持传递配置对象:

this.$notify({
  title: '提示',
  message: '这是一条通知消息',
  type: 'success'
});

通知类型

Notify 支持四种类型,通过 type 属性设置:

  • success:成功通知
  • warning:警告通知
  • info:信息通知
  • error:错误通知
this.$notify({
  title: '成功',
  message: '操作成功完成',
  type: 'success'
});

自定义位置

通过 position 属性可以设置通知显示的位置,默认为 top-right,可选值包括:

  • top-right
  • top-left
  • bottom-right
  • bottom-left
this.$notify({
  title: '提示',
  message: '自定义位置的通知',
  position: 'bottom-left'
});

设置持续时间

使用 duration 属性控制通知自动关闭的时间(毫秒),默认值为 4500。设置为 0 时不会自动关闭。

this.$notify({
  title: '提示',
  message: '5秒后关闭的通知',
  duration: 5000
});

自定义关闭按钮

通过 showClose 属性控制是否显示关闭按钮,默认为 true

this.$notify({
  title: '提示',
  message: '没有关闭按钮的通知',
  showClose: false
});

使用 HTML 内容

设置 dangerouslyUseHTMLStringtrue 可以在 message 中使用 HTML 内容。

this.$notify({
  title: 'HTML 内容',
  message: '<strong>这是加粗的</strong> <i>斜体文本</i>',
  dangerouslyUseHTMLString: true
});

全局配置

可以在引入 ElementUI 时全局配置 Notify 的默认行为:

import Vue from 'vue';
import ElementUI from 'element-ui';

Vue.use(ElementUI, {
  notify: {
    position: 'bottom-right',
    duration: 3000
  }
});

手动关闭通知

Notify 返回一个关闭函数,可以手动关闭通知:

const notifyInstance = this.$notify({
  title: '提示',
  message: '可以手动关闭的通知',
  duration: 0
});

// 3秒后手动关闭
setTimeout(() => {
  notifyInstance.close();
}, 3000);

完整配置选项

Notify 支持的完整配置选项包括:

  • title:标题
  • message:消息内容
  • type:类型
  • duration:显示时间
  • position:位置
  • showClose:是否显示关闭按钮
  • onClose:关闭时的回调函数
  • offset:偏移距离
  • customClass:自定义类名
this.$notify({
  title: '完整配置',
  message: '包含所有配置选项的通知',
  type: 'warning',
  duration: 5000,
  position: 'top-left',
  showClose: true,
  onClose: function() {
    console.log('通知已关闭');
  },
  offset: 50,
  customClass: 'my-notify'
});

单独引入 Notify

如果只需要使用 Notify 组件,可以单独引入:

import { Notification } from 'element-ui';

Notification.success({
  title: '成功',
  message: '单独引入的通知'
});

以上是 ElementUI Notify 组件的详细使用方法,根据实际需求选择合适的配置即可。

elementui notify

标签: elementuinotify
分享给朋友:

相关文章

elementui获取input的值

elementui获取input的值

获取 input 值的常用方法 在 Element UI 中,可以通过 v-model 双向绑定或 ref 引用的方式获取 input 组件的值。 使用 v-model 双向绑定 <…

elementui键盘

elementui键盘

Element UI 键盘事件处理 Element UI 是基于 Vue.js 的组件库,处理键盘事件通常结合 Vue 的原生事件绑定或自定义指令实现。 常用键盘事件绑定方式 在 Element…

elementui整合

elementui整合

ElementUI 整合指南 ElementUI 是一款基于 Vue.js 的桌面端组件库,广泛应用于中后台系统的快速开发。以下是整合 ElementUI 到项目中的具体方法: 安装 Elemen…

elementui核心

elementui核心

Element UI 核心概念 Element UI 是基于 Vue.js 2.0 的桌面端组件库,其核心设计理念是简洁、高效、易用。以下是其核心组成部分: 组件化设计 提供丰富的 UI 组件,如…

elementui选中

elementui选中

选中状态的基本用法 在Element UI中,选中状态通常通过v-model绑定数据实现。例如表格的多选功能,使用el-table组件配合type="selection"的列定义,结合v-model绑…

elementui抽屉

elementui抽屉

elementui抽屉的基本用法 Element UI的抽屉组件(Drawer)是一种从页面边缘滑出的面板,常用于显示额外内容或操作。需要在项目中引入Element UI库后使用。 安装Eleme…