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

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
分享给朋友:

相关文章

elementui require

elementui require

ElementUI 引入方式 ElementUI 可以通过多种方式引入到项目中,具体选择取决于项目需求和开发环境。 npm 安装 适用于 Vue.js 项目,通过 npm 或 yarn 安装 El…

elementui pattern

elementui pattern

ElementUI 表单验证 pattern 用法 ElementUI 的表单验证中,pattern 属性用于通过正则表达式自定义验证规则。结合 el-form 和 el-form-item 的 ru…

elementui textarea

elementui textarea

ElementUI Textarea 组件使用指南 ElementUI 提供了 el-textarea 组件用于多行文本输入,支持常见的表单功能如禁用状态、自适应高度、最大长度限制等。 基本用法 通…

elementui markdown

elementui markdown

ElementUI 与 Markdown 结合使用 ElementUI 是一个基于 Vue.js 的组件库,常用于快速构建前端界面。Markdown 是一种轻量级标记语言,常用于文档编写。以下是将 E…

elementui检验

elementui检验

ElementUI 表单验证方法 ElementUI 提供了强大的表单验证功能,主要通过 el-form 和 el-form-item 组件结合 rules 属性实现。以下是常见的验证方法: 基础表…

elementui formdata

elementui formdata

使用 ElementUI 上传文件时处理 FormData ElementUI 的 el-upload 组件常用于文件上传,结合 FormData 可以方便地处理文件数据。以下是实现方法: 创建 F…