7.7 模态交互组件详解

“模态交互组件”指的是那些会打断用户当前操作、需要用户响应后才能继续的组件。Element Plus 提供了四大核心模态交互组件:

  1. el-dialog - 对话框
  2. el-message-box - 消息弹框
  3. el-message - 消息提示
  4. el-notification - 通知

🎯 Element Plus 模态交互组件详解


一、el-dialog - 对话框(最常用)

用于展示复杂内容、表单、确认操作等。

✅ 基本结构
<template>
  <el-button @click="dialogVisible = true">打开对话框</el-button>

  <el-dialog
    v-model="dialogVisible"
    title="提示"
    width="30%"
    :before-close="handleClose"
  >
    <span>这是一段信息</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'

const dialogVisible = ref(false)

const handleClose = (done) => {
  ElMessageBox.confirm('确认关闭吗?')
    .then(() => done())
    .catch(() => {})
}

const confirm = () => {
  // 提交逻辑
  dialogVisible.value = false
}
</script>
🔧 核心属性
属性说明
v-model控制显示/隐藏(必填)
title标题
width宽度(支持 %px
top距离顶部的距离
fullscreen是否全屏
modal是否显示遮罩
append-to-bodyDialog 自身是否插入至 body 元素上
close-on-click-modal点击遮罩是否关闭
close-on-press-escape按 Esc 是否关闭
show-close是否显示右上角关闭按钮
before-close关闭前的回调(可阻止关闭)
💡 高级用法
  • 表单对话框:嵌入 el-form 实现新增/编辑弹窗。
  • 拖拽对话框:需结合第三方库如 vuedraggable
  • 居中显示align-center 属性使内容垂直居中。
<el-dialog center title="居中对话框">
  <p>内容垂直居中</p>
</el-dialog>

二、el-message-box - 消息弹框(确认/提示)

用于确认操作、输入提示、登录框等需要用户明确反馈的场景。

✅ 基本用法
import { ElMessageBox } from 'element-plus'

// 确认框
ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '警告', {
  confirmButtonText: '确定',
  cancelButtonText: '取消',
  type: 'warning'
})
.then(() => {
  ElMessage.success('删除成功')
})
.catch(() => {
  ElMessage.info('已取消删除')
})
🧩 类型
方法说明
ElMessageBox.alert(message, title?, options?)警告提示(只有确定按钮)
ElMessageBox.confirm(message, title?, options?)确认框(确定/取消)
ElMessageBox.prompt(message, title?, options?)输入框(带输入)
ElMessageBox.close()关闭所有 Message Box
🔧 常用配置项
ElMessageBox.confirm('内容', '标题', {
  type: 'warning',           // 类型:success/warning/info/error
  showCancelButton: true,    // 显示取消按钮
  confirmButtonText: '确定',
  cancelButtonText: '取消',
  closeOnClickModal: false,  // 点遮罩不关闭
  inputPlaceholder: '请输入内容',
  inputValidator: (value) => value.length > 2, // 输入校验
  inputErrorMessage: '内容过短'
})
💡 实战:输入密码确认
ElMessageBox.prompt('请输入管理员密码', '权限验证', {
  type: 'password',
  inputType: 'password',
  showCancelButton: true,
  confirmButtonText: '确认',
  cancelButtonText: '取消',
  beforeClose: (action, instance, done) => {
    if (action === 'confirm') {
      if (instance.inputValue === '123456') {
        done()
        ElMessage.success('验证通过')
      } else {
        ElMessage.error('密码错误')
      }
    } else {
      done()
    }
  }
})

三、el-message - 消息提示(轻量)

用于轻量级反馈,如操作成功、失败、警告等,不会阻塞用户操作

✅ 基本用法
import { ElMessage } from 'element-plus'

ElMessage('这是一条消息')
ElMessage.success('操作成功')
ElMessage.warning('警告内容')
ElMessage.error('出错了')
ElMessage.info('这是一条信息')
🔧 配置项
ElMessage({
  message: '自定义消息',
  type: 'success',
  duration: 3000,           // 显示时间(ms),0 为永久
  showClose: true,          // 显示关闭按钮
  center: true,             // 文字居中
  offset: 100,              // 距离顶部距离
  grouping: true,           // 相同内容合并
  onClose: () => console.log('关闭了')
})
🚫 注意
  • ElMessage 不是模态框,它只是提示,用户无需交互。
  • 不会阻止页面其他操作。

四、el-notification - 通知

类似 ElMessage,但出现在右上角,常用于系统通知、提醒

✅ 基本用法
import { ElNotification } from 'element-plus'

ElNotification({
  title: '提示',
  message: '这是一条通知',
  type: 'success',
  duration: 4000,
  position: 'top-right'
})

// 快捷方式
ElNotification.success('操作成功')
ElNotification.warning('警告')
🔧 配置项
属性说明
title标题
message内容
type类型
duration显示时间
position位置:top-righttop-leftbottom-rightbottom-left
showClose是否显示关闭按钮
onClose关闭回调

✅ 四大组件对比总结

组件类型是否阻塞操作用途调用方式
el-dialog模态框✅ 是复杂内容、表单、确认模板中使用 v-model
el-message-box模态弹框✅ 是确认、输入、警告ElMessageBox.xxx()
el-message轻提示❌ 否操作反馈ElMessage.xxx()
el-notification通知❌ 否系统提醒ElNotification.xxx()

🎯 使用场景推荐

场景推荐组件
新增/编辑表单el-dialog
删除确认el-message-box.confirm()
提交成功提示el-message.success()
系统有新消息el-notification
需要用户输入内容el-message-box.prompt()
只读信息提示el-message.info()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chxii

小小打赏,大大鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值