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

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 用于确认对话框、输入框等交互式提示。

确认框

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

输入框

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

通知提示(Notification)

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

基本用法

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 });

注意事项

elementui消息

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

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

相关文章

elementui读音

elementui读音

关于 ElementUI 的读音 ElementUI 的读音可以拆分为两部分: Element:读作 /ˈelɪmənt/,类似于“艾-利-门特” UI:读作 /ˌjuː ˈaɪ/,即字母“U”和“…

elementui抽屉

elementui抽屉

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

elementui import

elementui import

安装 Element UI 在项目中安装 Element UI 可以通过 npm 或 yarn 完成: npm install element-ui --save # 或 yarn add el…

elementui渐变

elementui渐变

实现ElementUI渐变效果的方法 ElementUI组件库本身不直接提供渐变样式,但可以通过CSS自定义实现渐变效果。以下是几种常见实现方式: 背景渐变 通过CSS的linear-gradien…

elementui api

elementui api

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

替代elementui

替代elementui

替代 ElementUI 的 UI 框架推荐 ElementUI 是基于 Vue 2.x 的组件库,若需替代方案,可考虑以下框架: Vue 2.x 兼容方案 1. Ant Design Vue…