1. MessageBox弹框

1.1. MessageBox弹框, 模拟系统的消息提示框而实现的一套模态对话框组件, 用于消息提示、确认消息和提交内容。

1.2. 从场景上说, MessageBox的作用是美化系统自带的alert、confirm和prompt, 因此适合展示较为简单的内容。如果需要弹出较为复杂的内容, 请使用Dialog。

1.3. Options

参数

说明

类型

可选值

默认值

title

MessageBox标题

string

message

MessageBox消息正文内容

string / VNode

dangerouslyUseHTMLString

是否将message属性作为HTML片段处理

boolean

false

type

消息类型, 用于显示图标

string

success / info / warning / error

iconClass

自定义图标的类名, 会覆盖type

string

customClass

MessageBox的自定义类名

string

callback

若不使用Promise, 可以使用此参数指定MessageBox关闭后的回调

function(action, instance), action的值为'confirm', 'cancel'或'close', instance为MessageBox实例, 可以通过它访问实例上的属性和方法

showClose

MessageBox是否显示右上角关闭按钮

boolean

true

beforeClose

MessageBox关闭前的回调, 会暂停实例的关闭

function(action, instance, done), action的值为'confirm', 'cancel'或'close'; instance为MessageBox实例, 可以通过它访问实例上的属性和方法; done用于关闭MessageBox实例

distinguishCancelAndClose

是否将取消(点击取消按钮)与关闭(点击关闭按钮或遮罩层、按下ESC键)进行区分

boolean

false

lockScroll

是否在MessageBox出现时将body滚动锁定

boolean

true

showCancelButton

是否显示取消按钮

boolean

false(以confirm和prompt方式调用时为true)

showConfirmButton

是否显示确定按钮

boolean

true

cancelButtonText

取消按钮的文本内容

string

取消

confirmButtonText

确定按钮的文本内容

string

确定

cancelButtonClass

取消按钮的自定义类名

string

confirmButtonClass

确定按钮的自定义类名

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

inputPattern

输入框的校验表达式

regexp

inputValidator

输入框的校验函数。可以返回布尔值或字符串, 若返回一个字符串, 则返回结果会被赋值给inputErrorMessage

function

inputErrorMessage

校验未通过时的提示文本

string

输入的数据不合法!

center

是否居中布局

boolean

false

roundButton

是否使用圆角按钮

boolean

false

2. MessageBox弹框全局方法

2.1. 如果你完整引入了Element, 它会为Vue.prototype添加如下全局方法: $msgbox, $alert, $confirm和$prompt。因此在Vue instance中可以采用本页面中的方式调用MessageBox。调用参数为:

$msgbox(options)
$alert(message, title, options) 或 $alert(message, options)
$confirm(message, title, options) 或 $confirm(message, options)
$prompt(message, title, options) 或 $prompt(message, options)

3. MessageBox弹框单独引用

3.1. 如果单独引入MessageBox:

import { MessageBox } from 'element-ui';MessageBox(options)
MessageBox.alert(message, title, options) 或 MessageBox.alert(message, options)
MessageBox.confirm(message, title, options) 或 MessageBox.confirm(message, options)
MessageBox.prompt(message, title, options) 或 MessageBox.prompt(message, options)

4. MessageBox弹框例子

4.1. 使用脚手架新建一个名为element-ui-messagebox的前端项目, 同时安装Element插件。

4.2. 编辑index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import AlertMessageBox from '../components/AlertMessageBox.vue'
import ConfirmMessageBox from '../components/ConfirmMessageBox.vue'
import PromptMessageBox from '../components/PromptMessageBox.vue'
import Msgbox from '../components/Msgbox.vue'
import HtmlMessageBox from '../components/HtmlMessageBox.vue'
import CancelCloseMessageBox from '../components/CancelCloseMessageBox.vue'
import CenterMessageBox from '../components/CenterMessageBox.vue'Vue.use(VueRouter)const routes = [{ path: '/', redirect: '/AlertMessageBox' },{ path: '/AlertMessageBox', component: AlertMessageBox },{ path: '/ConfirmMessageBox', component: ConfirmMessageBox },{ path: '/PromptMessageBox', component: PromptMessageBox },{ path: '/Msgbox', component: Msgbox },{ path: '/HtmlMessageBox', component: HtmlMessageBox },{ path: '/CancelCloseMessageBox', component: CancelCloseMessageBox },{ path: '/CenterMessageBox', component: CenterMessageBox }
]const router = new VueRouter({routes
})export default router

4.3. 在components下创建AlertMessageBox.vue

<template><div><h1>消息提示</h1><h4>调用$alert方法即可打开消息提示, 它模拟了系统的alert, 无法通过按下ESC或点击框外关闭。此例中接收了两个参数, message和title。值得一提的是, 窗口被关闭后, 它默认会返回一个Promise对象便于进行后续操作的处理。若不确定浏览器是否支持Promise, 可自行引入第三方polyfill或像本例一样使用回调进行后续处理。</h4><el-button type="text" @click="open">点击打开 Message Box</el-button></div>
</template><script>
export default {methods: {open () {this.$alert('这是一段内容', '标题名称', {confirmButtonText: '确定',callback: action => {this.$message({type: 'info',message: `action: ${action}`})}})}}
}
</script>

4.4. 在components下创建ConfirmMessageBox.vue

<template><div><h1>确认消息</h1><h4>调用$confirm方法即可打开消息提示, 它模拟了系统的confirm。Message Box组件也拥有极高的定制性, 我们可以传入options作为第三个参数, 它是一个字面量对象。type字段表明消息类型, 可以为success、error、info和warning, 无效的设置将会被忽略。注意, 第二个参数title必须定义为String类型, 如果是Object, 会被理解为options。在这里我们用了Promise来处理后续响应。</h4><el-button type="text" @click="open">点击打开 Message Box</el-button></div>
</template><script>
export default {methods: {open () {this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}).then(() => {this.$message({type: 'success',message: '删除成功!'})}).catch(() => {this.$message({type: 'info',message: '已取消删除'})})}}
}
</script>

4.5. 在components下创建PromptMessageBox.vue

<template><div><h1>提交内容</h1><h4>调用$prompt方法即可打开消息提示, 它模拟了系统的prompt。可以用inputPattern字段自己规定匹配模式, 或者用inputValidator规定校验函数, 可以返回Boolean或String, 返回false或字符串时均表示校验未通过, 同时返回的字符串相当于定义了inputErrorMessage字段。此外, 可以用inputPlaceholder字段来定义输入框的占位符。</h4><el-button type="text" @click="open">点击打开 Message Box</el-button></div>
</template><script>
export default {methods: {open () {this.$prompt('请输入邮箱', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,inputErrorMessage: '邮箱格式不正确'}).then(({ value }) => {this.$message({type: 'success',message: '你的邮箱是: ' + value})}).catch(() => {this.$message({type: 'info',message: '取消输入'})})}}
}
</script>

4.6. 在components下创建Msgbox.vue

<template><div><h1>自定义</h1><h4>以上三个方法都是对$msgbox方法的再包装。本例直接调用$msgbox方法, 使用了showCancelButton字段,用于显示取消按钮。另外可使用cancelButtonClass为其添加自定义样式,使用cancelButtonText来自定义按钮文本(Confirm按钮也具有相同的字段, 在文末的字段说明中有完整的字段列表)。此例还使用了beforeClose属性, 它的值是一个方法, 会在MessageBox的实例关闭前被调用, 同时暂停实例的关闭。它有三个参数: action、实例本身和done方法。使用它能够在关闭前对实例进行一些操作, 比如为确定按钮添加loading状态等; 此时若需要关闭实例, 可以调用done方法(若在beforeClose中没有调用done, 则实例不会关闭)。</h4><el-button type="text" @click="open">点击打开 Message Box</el-button></div>
</template><script>
export default {methods: {open () {const h = this.$createElementthis.$msgbox({title: '消息',message: h('p', null, [h('span', null, '内容可以是 '),h('i', { style: 'color: teal' }, 'VNode')]),showCancelButton: true,confirmButtonText: '确定',cancelButtonText: '取消',beforeClose: (action, instance, done) => {if (action === 'confirm') {instance.confirmButtonLoading = trueinstance.confirmButtonText = '执行中...'setTimeout(() => {done()setTimeout(() => {instance.confirmButtonLoading = false}, 300)}, 3000)} else {done()}}}).then(action => {this.$message({type: 'info',message: 'action: ' + action})})}}
}
</script>

4.7. 在components下创建HtmlMessageBox.vue

<template><div><h1>使用HTML片段</h1><h4>将dangerouslyUseHTMLString属性设置为true, message就会被当作HTML片段处理。</h4><el-button type="text" @click="open">点击打开 Message Box</el-button></div>
</template><script>
export default {methods: {open () {this.$alert('<strong>这是 <i>HTML</i> 片段</strong>', 'HTML 片段', {dangerouslyUseHTMLString: true})}}
}
</script>

4.8. 在components下创建CancelCloseMessageBox.vue

<template><div><h1>区分取消与关闭-有些场景下, 点击取消按钮与点击关闭按钮有着不同的含义</h1><h4>默认情况下, 当用户触发取消(点击取消按钮)和触发关闭(点击关闭按钮或遮罩层、按下ESC键)时, Promise的reject回调和callback回调的参数均为'cancel'。如果将distinguishCancelAndClose属性设置为true, 则上述两种行为的参数分别为'cancel'和'close'。</h4><el-button type="text" @click="open">点击打开 Message Box</el-button></div>
</template><script>
export default {methods: {open () {this.$confirm('检测到未保存的内容,是否在离开页面前保存修改?', '确认信息', {distinguishCancelAndClose: true,confirmButtonText: '保存',cancelButtonText: '放弃修改'}).then(() => {this.$message({type: 'info',message: '保存修改'})}).catch(action => {this.$message({type: 'info',message: action === 'cancel'? '放弃保存并离开页面': '停留在当前页面'})})}}
}
</script>

4.9. 在components下创建CenterMessageBox.vue

<template><div><h1>居中布局</h1><h4>将center设置为true即可开启居中布局</h4><el-button type="text" @click="open">点击打开 Message Box</el-button></div>
</template><script>
export default {methods: {open () {this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning',center: true}).then(() => {this.$message({type: 'success',message: '删除成功!'})}).catch(() => {this.$message({type: 'info',message: '已取消删除'})})}}
}
</script>

4.10. 运行项目, 访问http://localhost:8080/#/AlertMessageBox

4.11. 运行项目, 访问http://localhost:8080/#/ConfirmMessageBox

4.12. 运行项目, 访问http://localhost:8080/#/PromptMessageBox

4.13. 运行项目, 访问http://localhost:8080/#/Msgbox

4.14. 运行项目, 访问http://localhost:8080/#/HtmlMessageBox

4.15. 运行项目, 访问http://localhost:8080/#/CancelCloseMessageBox

4.16. 运行项目, 访问http://localhost:8080/#/CenterMessageBox

031_MessageBox弹框相关推荐

  1. Android Dialog 弹框之外的区域 默认透明背景色修改

    就是下面弹框之外的颜色值 透明度修改 一般弹框外的区域的不是全透明  都有一个默认的值使用的属性为 setDimAmount 这个默认值是0.5 这个值的范围在 0-1 之间 如果设置为1 就是全黑色 ...

  2. popWindow 出现点击上下空白区域消失点击左右空白区域不消失弹框的处理方法

    一般默认是消失的,但是自己的项目中出现点击上下弹框可以消失,点击左右弹框不消失 最后自己发现了问题如下 popupWindow = new PopupWindow(brief_view, FrameL ...

  3. Android webview 加载的html 无法显示弹框

    WebSettings webSettings = webView.getSettings(); // 设置与Js交互的权限 webSettings.setJavaScriptEnabled(true ...

  4. android 底部弹框 BottomSheetDialog 的使用

    先简单的说下普通弹框使用 步骤 首先项目需要添加design 库 简单的使用和dialog 一样 如下 BottomSheetDialog sheelt = new BottomSheetDialog ...

  5. Android 9.0 系统弹框

    处理方法:就是屏蔽弹框就行了 private void closeAndroidPDialog(){try {Class aClass = Class.forName("android.co ...

  6. vue 定义全局弹框_VUE路由拦截:Vue自定义全局弹窗组件

    前言 在任何一个平台中,如果需要增加用户黏度,除了用户需要的基本内容外,用户登录注册提交信息也是非常重要的一环,可以了解用户基本信息,用户喜欢等. 抛出前后端混合开发外,vue可以轻松的实现路由拦截. ...

  7. android fragment 弹出对话框,Android DialogFragment弹框实现

    前言: 网上说了一堆好处,我只说自己深有体会的两点吧 1.屏幕横竖屏切换的时候,弹框可以保存状态 2.可以在popwindow中使用 使用步骤 1.创建继承DialogFragment的dialogF ...

  8. pythonselenium浮动框_python上selenium的弹框操作实现

    selenium之弹框操作 1,分类 弹框类型自见解分为四种: 1,页面弹框 2,警告提示框(alert) 3,确认消息框(confirm) 4,提示消息对话(prompt) 提示: selenium ...

  9. element-- 修改MessageBox 弹框 中确定和取消按钮顺序

    需求:修改弹框中的 取消/确定按钮顺序,及头部和底部背景颜色; 原ui效果图 需求ui效果图 方法:对取消及确定按钮自定义类名,样式重写 转载于:https://www.cnblogs.com/xin ...

最新文章

  1. 印度不只有开挂火车,还有一开挂的数学家,凭一己之力单刷数学界
  2. STM32应用笔记转载
  3. c语言做简单的水果店程序,怎么开发一款生鲜水果小程序?水果店+小程序该如何组合运营?...
  4. 【转】TCP状态变迁图
  5. 5G技术对我们生活的影响
  6. PCR概述及前沿技术
  7. 谷歌浏览器打印不弹出预览直接打印机打印的方法
  8. Platform机制
  9. DNS云学堂 | 权威DNS那些事儿(上)
  10. AX3600开启SSH功能
  11. 2020年年假期间规划
  12. 互联网告别免费时代,准备好了吗?
  13. js下流媒体的在线播放
  14. set feedback on
  15. 前端开发小白,如何找一个师傅?
  16. Twitter API: Door To Social Media Analysis I
  17. XP停止服务:不必难过 千里相送终有一别
  18. 企业云协作平台--定位
  19. 微软WebCast(视频教材下载工具)上千视频下载
  20. 深度解读AI从业者必备算法和工具 -- 公开课

热门文章

  1. 局域网IP-MAC绑定方案
  2. 转载: Vim 练级攻略
  3. DICKSON隆重推荐的博客 --- SAP
  4. iOS 中捕获程序崩溃日志
  5. 画蛇添足之error of activesync over usb link to pc
  6. PHP中常用的正则表达式函数
  7. UTL_FILE包用法小记
  8. 871 最低加油次数
  9. 数位dp:Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum
  10. 蓝桥杯Java输入输出相关