Skip to content

Message Box 消息弹出框

Message Box 用于展示需要用户确认或输入的模态消息。VISTA B 直接使用 Element Plus 的 ElMessageBox 服务,适合删除确认、风险提示、二次确认和简单文本输入等场景。

基础用法

使用 ElMessageBox.alert 展示只需要确认的消息弹框。

vue
<template>
  <el-button type="primary" @click="open">打开消息弹框</el-button>
</template>

<script setup lang="ts">
import { ElMessageBox } from 'element-plus'

const open = () => {
  ElMessageBox.alert('项目配置已保存,可以继续编辑或返回列表。', '操作提示', {
    confirmButtonText: '知道了',
    type: 'success',
  })
}
</script>

确认消息

使用 ElMessageBox.confirm 展示带取消按钮的确认弹框。确认时 Promise resolve,取消或关闭时 Promise reject。

vue
<template>
  <el-button type="danger" @click="remove">删除项目</el-button>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'

const remove = () => {
  ElMessageBox.confirm('删除后数据不可恢复,是否继续?', '删除项目', {
    confirmButtonText: '删除',
    cancelButtonText: '取消',
    confirmButtonType: 'danger',
    type: 'warning',
  })
    .then(() => {
      ElMessage.success('已删除')
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}
</script>

提交内容

使用 ElMessageBox.prompt 展示输入框。可以通过 inputPatterninputValidator 校验输入内容。

vue
<template>
  <el-button type="primary" @click="submit">提交审批备注</el-button>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'

const submit = () => {
  ElMessageBox.prompt('请输入本次审批备注', '提交审批', {
    confirmButtonText: '提交',
    cancelButtonText: '取消',
    inputPlaceholder: '审批备注',
    inputPattern: /\S+/,
    inputErrorMessage: '备注不能为空',
  }).then(({ value }) => {
    ElMessage.success(`已提交:${value}`)
  })
}
</script>

使用 HTML 字符串

设置 dangerouslyUseHTMLString 后,message 会被当作 HTML 片段渲染。该能力只应使用可信内容。

vue
<script setup lang="ts">
import { ElMessageBox } from 'element-plus'

const open = () => {
  ElMessageBox.alert(
    '<strong>报价单已生成</strong><br />请确认金额和交付日期后再发送客户。',
    '生成成功',
    {
      dangerouslyUseHTMLString: true,
      confirmButtonText: '去确认',
      type: 'success',
    },
  )
}
</script>

自定义内容

message 支持传入 VNode,也可以传入函数并接收 confirmcancelclose 操作方法。

vue
<script setup lang="ts">
import { h } from 'vue'
import { ElMessageBox } from 'element-plus'

const open = () => {
  ElMessageBox({
    title: '分配成员',
    message: h('div', { style: 'line-height: 1.7' }, [
      h('p', { style: 'margin: 0 0 8px' }, '即将把 3 个待办任务分配给当前成员。'),
      h('strong', { style: 'color: var(--el-color-primary)' }, '确认后会发送站内通知。'),
    ]),
    confirmButtonText: '分配',
    cancelButtonText: '稍后处理',
    showCancelButton: true,
    type: 'info',
  })
}
</script>

自定义关闭前逻辑

beforeClose 可以拦截关闭流程,适合在确认按钮中执行异步操作或展示 loading 状态。

vue
<script setup lang="ts">
import { ElMessage, ElMessageBox, type Action, type MessageBoxState } from 'element-plus'

const open = () => {
  ElMessageBox.confirm('发布后将同步到线上环境,是否继续?', '发布版本', {
    confirmButtonText: '发布',
    cancelButtonText: '取消',
    type: 'warning',
    beforeClose: (action: Action, instance: MessageBoxState, done: () => void) => {
      if (action === 'confirm') {
        instance.confirmButtonLoading = true
        instance.confirmButtonText = '发布中...'
        setTimeout(() => {
          done()
          ElMessage.success('发布成功')
        }, 1200)
        return
      }

      done()
    },
  })
}
</script>

区分取消与关闭

设置 distinguishCancelAndClose 后,点击取消按钮会返回 cancel,点击关闭按钮或遮罩会返回 close

vue
<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'

const open = () => {
  ElMessageBox.confirm('页面上还有未保存的修改,确定要离开吗?', '离开编辑', {
    confirmButtonText: '保存并离开',
    cancelButtonText: '放弃更改',
    distinguishCancelAndClose: true,
    type: 'warning',
  })
    .then(() => {
      ElMessage.success('已保存')
    })
    .catch((action: string) => {
      ElMessage.info(action === 'close' ? '继续编辑' : '已放弃修改')
    })
}
</script>

居中布局

设置 center 后标题、内容和按钮会居中展示,适合更强调结果反馈的场景。

vue
<script setup lang="ts">
import { ElMessageBox } from 'element-plus'

const open = () => {
  ElMessageBox.alert('本次巡检未发现异常,可以继续执行下一步。', '巡检完成', {
    center: true,
    confirmButtonText: '知道了',
    type: 'success',
  })
}
</script>

自定义图标

通过 icon 替换默认类型图标。传入组件时建议使用 markRaw,避免组件被响应式代理。

vue
<script setup lang="ts">
import { markRaw } from 'vue'
import { ElMessageBox } from 'element-plus'
import { InfoFilled } from '@element-plus/icons-vue'

const open = () => {
  ElMessageBox.alert('该策略会影响所有子账号,请确认后继续。', '策略提示', {
    icon: markRaw(InfoFilled),
    confirmButtonText: '确认',
    type: 'info',
  })
}
</script>

可拖拽弹框

设置 draggable 后可以拖拽弹框。设置 overflow 后,拖拽时弹框可以超出视口边界。

vue
<script setup lang="ts">
import { ElMessageBox } from 'element-plus'

const open = () => {
  ElMessageBox.alert('按住标题栏可以拖动弹框位置。', '可拖拽弹框', {
    draggable: true,
    overflow: true,
    confirmButtonText: '完成',
  })
}
</script>

API

MessageBox 方法

方法说明类型
ElMessageBox(options)打开一个消息弹框Function
ElMessageBox.alert(message, title, options)打开提示弹框,仅展示确认按钮Function
ElMessageBox.confirm(message, title, options)打开确认弹框,默认展示确认和取消按钮Function
ElMessageBox.prompt(message, title, options)打开输入弹框,默认展示输入框、确认和取消按钮Function
ElMessageBox.close()关闭当前所有 MessageBoxFunction

MessageBox 配置

属性说明类型默认值
title弹框标题string-
message弹框内容string | VNode | function-
type消息类型,用于显示默认图标primary / success / warning / info / error-
icon自定义图标组件string | Component-
closeIcon自定义关闭图标组件string | Component-
customClass弹框自定义 classstring-
customStyle弹框自定义内联样式object{}
modal是否显示遮罩booleantrue
modalClass遮罩自定义 classstring-
lockScroll是否在弹框出现时锁定 body 滚动booleantrue
showClose是否显示右上角关闭按钮booleantrue
closeOnClickModal是否可通过点击遮罩关闭booleantrue
closeOnPressEscape是否可通过 ESC 关闭booleantrue
closeOnHashChangehash 变化时是否关闭弹框booleantrue
showConfirmButton是否显示确认按钮booleantrue
showCancelButton是否显示取消按钮booleanfalse
confirmButtonText确认按钮文本string确定
cancelButtonText取消按钮文本string取消
confirmButtonType确认按钮类型primary / success / warning / danger / info / textprimary
cancelButtonType取消按钮类型primary / success / warning / danger / info / text-
confirmButtonLoadingIcon确认按钮 loading 图标string | ComponentLoading
cancelButtonLoadingIcon取消按钮 loading 图标string | ComponentLoading
confirmButtonClass确认按钮自定义 classstring-
cancelButtonClass取消按钮自定义 classstring-
confirmButtonDisabled是否禁用确认按钮booleanfalse
buttonSize自定义按钮尺寸large / default / small-
roundButton是否使用圆角按钮booleanfalse
center是否居中布局booleanfalse
draggable是否可拖拽弹框booleanfalse
overflow拖拽时是否允许弹框超出视口booleanfalse
dangerouslyUseHTMLString是否将 message 当作 HTML 字符串处理booleanfalse
distinguishCancelAndClose是否区分取消和关闭操作booleanfalse
beforeClose弹框关闭前的回调,会暂停关闭流程function-
callback若不使用 Promise,可通过回调接收关闭动作function-
appendTo指定弹框挂载的元素HTMLElement | stringbody

Prompt 配置

属性说明类型默认值
showInput是否显示输入框booleanfalse
inputPlaceholder输入框占位文本string-
inputValue输入框初始值string-
inputType输入框类型stringtext
inputPattern输入框校验正则RegExp-
inputValidator输入框校验函数,返回 false 或错误文本时校验失败function-
inputErrorMessage输入框校验失败时的提示文本string-

Promise 返回

操作结果说明
点击确认resolve('confirm')alertconfirm 默认返回确认动作
输入后确认resolve({ value, action })prompt 会返回输入值和动作
点击取消reject('cancel')可在 catch 中处理取消操作
点击关闭reject('close')仅在 distinguishCancelAndClosetrue 时区分关闭动作

HOMEVISTA 设计规范