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

elementui消息

2026-01-14 20:02:21前端教程

ElementUI 消息提示

ElementUI 提供了一套消息提示组件,包括 MessageMessageBoxNotification,用于展示不同类型的反馈信息。

消息提示(Message)

Message 用于显示全局的轻量级提示,通常用于操作反馈。

基本用法

this.$message('这是一条普通消息');
this.$message.success('操作成功');
this.$message.warning('警告提示');
this.$message.error('错误提示');
this.$message.info('信息提示');

自定义配置

this.$message({
  message: '自定义消息',
  type: 'success',
  duration: 3000,  // 显示时长(毫秒)
  showClose: true,  // 是否显示关闭按钮
  center: true      // 文字居中
});

消息弹框(MessageBox)

MessageBox 用于确认对话框、输入框等交互式提示。

elementui消息

确认框

this.$confirm('确认删除吗?', '提示', {
  confirmButtonText: '确定',
  cancelButtonText: '取消',
  type: 'warning'
}).then(() => {
  // 用户点击确认
}).catch(() => {
  // 用户点击取消
});

输入框

this.$prompt('请输入内容', '提示', {
  inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+/,  // 输入校验正则
  inputErrorMessage: '格式不正确'
}).then(({ value }) => {
  // 用户输入内容
}).catch(() => {
  // 用户取消
});

通知提示(Notification)

Notification 用于右上角弹出通知,适用于系统级消息。

elementui消息

基本用法

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

自定义配置

this.$notify({
  title: '警告',
  message: '请注意操作风险',
  type: 'warning',
  position: 'bottom-right',  // 显示位置
  offset: 50                 // 偏移量
});

全局引入与配置

main.js 中全局引入并配置默认参数:

import { Message, MessageBox, Notification } from 'element-ui';

Vue.prototype.$message = Message;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;

// 设置默认时长
Message.config({ duration: 2000 });
Notification.config({ duration: 4000 });

注意事项

  • 消息提示默认自动关闭,可通过 duration 调整显示时间。
  • MessageBox 返回 Promise,需正确处理用户交互。
  • 在组件内可通过 this.$message 调用,非组件环境需直接使用 Message

标签: 消息elementui
分享给朋友:

相关文章

uniapp消息推送

uniapp消息推送

uniapp消息推送实现方法 uniapp支持多种消息推送方式,包括uniPush、个推、极光推送等。以下为常见实现方案: uniPush(官方推荐) uniapp官方提供的推送服务,基于DClou…

elementui中文网

elementui中文网

Element UI 中文网相关信息 Element UI 是一款基于 Vue.js 的开源 UI 组件库,由饿了么前端团队开发和维护。以下是关于 Element UI 中文网的相关信息: 官方网…

vue elementui

vue elementui

Vue 中使用 Element UI Element UI 是一套基于 Vue 2.0 的桌面端组件库,提供丰富的 UI 组件和交互功能。以下是如何在 Vue 项目中集成和使用 Element UI…

elementui视频

elementui视频

以下是关于 ElementUI 视频资源的整理,涵盖教程、实战案例和官方资源: ElementUI 官方视频资源 ElementUI 官方文档虽以文字为主,但部分社区或第三方平台可能有官方团队发布的…

elementui iview

elementui iview

Element UI 和 iView 对比 Element UI 和 iView 都是基于 Vue.js 的 UI 组件库,广泛应用于企业级中后台系统的开发。以下是两者的主要特点和差异: Eleme…

elementui router

elementui router

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