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

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 组件,可以单独引入:

elementui notify

import { Notification } from 'element-ui';

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

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

标签: elementuinotify
分享给朋友:

相关文章

elementui table

elementui table

ElementUI Table 基本用法 ElementUI 的 Table 组件用于展示结构化数据,支持排序、筛选、分页等功能。以下是一个基础示例: <template> &l…

iview elementui

iview elementui

iview与Element UI对比 iview和Element UI都是基于Vue.js的UI组件库,适用于快速开发企业级中后台产品。以下是两者的主要差异和特点: 设计风格 iview采用圆角设…

elementui router

elementui router

Element UI 结合 Vue Router 的使用方法 Element UI 是一个基于 Vue.js 的组件库,而 Vue Router 是 Vue.js 的官方路由管理器。将两者结合使用可以…

elementui sass

elementui sass

在 Element UI 中使用 Sass Element UI 支持通过 Sass 进行样式定制,可以通过修改变量或覆盖样式来实现主题定制或组件样式调整。 安装 Sass 相关依赖: npm i…

vite elementui

vite elementui

使用 Vite 集成 Element Plus Element Plus 是 Element UI 的 Vue 3 版本,与 Vite 兼容性良好。以下是集成步骤: 安装依赖: npm insta…

elementui和

elementui和

ElementUI 简介 ElementUI 是一套基于 Vue.js 的桌面端组件库,由饿了么前端团队开发并开源。它提供了丰富的 UI 组件(如表格、表单、弹窗等),适用于快速构建企业级中后台管理系…