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

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按需

elementui按需

ElementUI 按需引入方法 ElementUI 支持按需引入组件,以减少最终打包体积。以下是具体实现方式: 安装 babel-plugin-component 需要安装官方推荐的 babel…

elementui ref

elementui ref

ElementUI 中使用 ref 的方法 在 ElementUI 中,ref 主要用于获取组件实例或 DOM 元素的引用,方便直接调用组件方法或操作 DOM。以下是几种常见的使用场景和示例:…

elementui echarts

elementui echarts

ElementUI 与 ECharts 集成指南 ElementUI 是饿了么团队开发的 Vue.js 组件库,ECharts 是百度开源的 JavaScript 图表库。两者结合可快速构建数据可视化…

elementui button

elementui button

基本用法 Element UI 的 Button 组件提供多种样式和功能,可以通过 type 属性设置按钮类型。常见类型包括 primary、success、info、warning、danger。…

elementui实例

elementui实例

以下是关于 Element UI 实例的整理内容,涵盖常见组件用法和实际开发中的关键点: 安装与基础配置 通过 npm 安装 Element UI: npm install element-u…

elementui api

elementui api

以下是 Element UI 常用 API 的整理说明,按照组件分类呈现: Form 表单 model 绑定表单数据对象rules 表单验证规则validate 方法用于整体表单验证resetFi…