MessageBox 弹框
# MessageBox 弹框
模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示、确认消息和提交内容。
从场景上说,MessageBox 的作用是美化系统自带的 alert、confirm 和 prompt,因此适合展示较为简单的内容。如果需要弹出较为复杂的内容,请使用 Dialog。
# 消息提示
当用户进行操作时会被触发,该对话框中断用户操作,直到用户确认知晓后才可关闭。
# 确认消息
提示用户确认其已经触发的动作,并询问是否进行此操作时会用到此对话框。
# 提交内容
当用户进行操作时会被触发,中断用户操作,提示用户进行输入的对话框。
# 自定义
可自定义配置不同内容。
# 使用 HTML 片段
content 属性支持传入 HTML 片段
content 属性虽然支持传入 HTML 片段,但是在网站上动态渲染任意 HTML 是非常危险的,因为容易导致 XSS 攻击 (opens new window)。因此在 dangerouslyUseHTMLString 打开的情况下,请确保 content 的内容是可信的,永远不要将用户提交的内容赋值给 content 属性。
# 居中布局
内容支持居中布局
# 局部提示
支持局部div弹出MessageBox
# 全局方法
如果你完整引入了 Element,它会为 Vue.prototype 添加如下全局方法:$aui.msgbox.show, $aui.alert.show, $aui.confirm.show 和 $aui.prompt.show。因此在 Vue instance 中可以采用本页面中的方式调用 MessageBox。调用参数为:
$aui.msgbox.show(options)$aui.alert.show(content, title, options)或$aui.alert.show(content, options)$aui.confirm.show(content, title, options)或$aui.confirm.show(content, options)$aui.prompt.show(content, title, options)或$aui.prompt.show(content, options)
# 单独引用
如果单独引入 MessageBox:
import { MessageBox } from 'element-ui';
那么对应于上述四个全局方法的调用方法依次为:MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt,调用参数与全局方法相同。
# Options
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 
|---|---|---|---|---|
| title | MessageBox 标题 | string | — | — | 
| content | MessageBox 消息正文内容 | string / VNode | — | — | 
| dangerouslyUseHTMLString | 是否将 content 属性作为 HTML 片段处理 | boolean | — | false | 
| type | 消息类型,用于显示图标 | string | success / info / warning / error | — | 
| target | 将MessageBox和遮罩层加在传入的dom元素上 | HTMLDivElement | ||
| duration | 消息类型,用于显示图标(message可用) | number | 4500 | |
| position | 自定义弹出位置 。type=notify生效 | string | top-right/top-left/bottom-right/bottom-left | — | 
| callback | 若不使用 Promise,可以使用此参数指定 MessageBox 关闭后的回调 | function(action, instance),action 的值为'confirm'或'cancel', instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法 | — | — | 
| showClose | MessageBox 是否显示右上角关闭按钮 | boolean | — | true | 
| beforeClose | MessageBox 关闭前的回调,会暂停实例的关闭 | function(action, instance, done),action 的值为'confirm'或'cancel';instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法;done 用于关闭 MessageBox 实例 | — | — | 
| showCancelButton | 是否显示取消按钮 | boolean | — | false(以 confirm 和 prompt 方式调用时为 true) | 
| showConfirmButton | 是否显示确定按钮 | boolean | — | true | 
| initFocusIndex | 初始化获取焦点的按钮 | number | 0: 取消; 1:确定 | 1 | 
| cancelButtonText | 取消按钮的文本内容 | string | — | 取消 | 
| confirmButtonText | 确定按钮的文本内容 | string | — | 确定 | 
| closeOnClickModal | 是否可通过点击遮罩关闭 MessageBox | boolean | — | true(以 alert 方式调用时为 false) | 
| closeOnPressEscape | 是否可通过按下 ESC 键关闭 MessageBox | boolean | — | true(以 alert 方式调用时为 false) | 
| closeOnHashChange | 是否在 hashchange 时关闭 MessageBox | boolean | — | true | 
| showInput | 是否显示输入框 | boolean | — | false(以 prompt 方式调用时为 true) | 
| inputPlaceholder | 输入框的占位符 | string | — | — | 
| inputType | 输入框的类型 | string | — | text | 
| inputValue | 输入框的初始文本 | string | — | — | 
| inputValidator | 输入框的校验函数。可以返回布尔值或字符串,若返回一个字符串, 则返回结果会被赋值给 inputErrorMessage | function | — | — | 
| inputErrorMessage | 校验未通过时的提示文本 | string | — | 输入的数据不合法! | 
| drag | 是否可拖动(拖动触发在头部) | boolean | — | false | 
| isShowIframe | 是否添加一个遮罩层大小的iframe,防止messagebox被遮挡 | boolean | — | false | 
# Methods
| 方法名 | 说明 | 参数 | 
|---|---|---|
| close | type=notify时,关闭当前的 Notification | — | 
