MessageBox 弹框
模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示、确认消息和提交内容。
alert
、confirm
和 prompt
,因此适合展示较为简单的内容。如果需要弹出较为复杂的内容,请使用 Dialog。消息提示
当用户进行操作时会被触发,该对话框中断用户操作,直到用户确认知晓后才可关闭。
调用$alert
方法即可打开消息提示,它模拟了系统的 alert
,无法通过按下 ESC 或点击框外关闭。此例中接收了两个参数,message
和title
。值得一提的是,窗口被关闭后,它默认会返回一个Promise
对象便于进行后续操作的处理。若不确定浏览器是否支持Promise
,可自行引入第三方 polyfill 或像本例一样使用回调进行后续处理。
- <template>
- <el-button type="text" @click="open">点击打开 Message Box</el-button>
- </template>
- <script>
- export default {
- methods: {
- open() {
- this.$alert('这是一段内容', '标题名称', {
- confirmButtonText: '确定',
- callback: action => {
- this.$message({
- type: 'info',
- message: `action: ${ action }`
- });
- }
- });
- }
- }
- }
- </script>
确认消息
提示用户确认其已经触发的动作,并询问是否进行此操作时会用到此对话框。
调用$confirm
方法即可打开消息提示,它模拟了系统的 confirm
。Message Box 组件也拥有极高的定制性,我们可以传入options
作为第三个参数,它是一个字面量对象。type
字段表明消息类型,可以为success
,error
,info
和warning
,无效的设置将会被忽略。注意,第二个参数title
必须定义为String
类型,如果是Object
,会被理解为options
。在这里我们用了 Promise 来处理后续响应。
- <template>
- <el-button type="text" @click="open2">点击打开 Message Box</el-button>
- </template>
- <script>
- export default {
- methods: {
- open2() {
- this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning'
- }).then(() => {
- this.$message({
- type: 'success',
- message: '删除成功!'
- });
- }).catch(() => {
- this.$message({
- type: 'info',
- message: '已取消删除'
- });
- });
- }
- }
- }
- </script>
提交内容
当用户进行操作时会被触发,中断用户操作,提示用户进行输入的对话框。
调用$prompt
方法即可打开消息提示,它模拟了系统的 prompt
。可以用inputPattern
字段自己规定匹配模式,或者用inputValidator
规定校验函数,可以返回Boolean
或String
,返回false
或字符串时均表示校验未通过,同时返回的字符串相当于定义了inputErrorMessage
字段。此外,可以用inputPlaceholder
字段来定义输入框的占位符。
- <template>
- <el-button type="text" @click="open3">点击打开 Message Box</el-button>
- </template>
- <script>
- export default {
- methods: {
- open3() {
- 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>
自定义
可自定义配置不同内容。
以上三个方法都是对$msgbox
方法的再包装。本例直接调用$msgbox
方法,使用了showCancelButton
字段,用于显示取消按钮。另外可使用cancelButtonClass
为其添加自定义样式,使用cancelButtonText
来自定义按钮文本(Confirm 按钮也具有相同的字段,在文末的字段说明中有完整的字段列表)。此例还使用了beforeClose
属性,它的值是一个方法,会在 MessageBox 的实例关闭前被调用,同时暂停实例的关闭。它有三个参数:action
、实例本身和done
方法。使用它能够在关闭前对实例进行一些操作,比如为确定按钮添加loading
状态等;此时若需要关闭实例,可以调用done
方法(若在beforeClose
中没有调用done
,则实例不会关闭)。
- <template>
- <el-button type="text" @click="open4">点击打开 Message Box</el-button>
- </template>
- <script>
- export default {
- methods: {
- open4() {
- const h = this.$createElement;
- this.$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 = true;
- instance.confirmButtonText = '执行中...';
- setTimeout(() => {
- done();
- setTimeout(() => {
- instance.confirmButtonLoading = false;
- }, 300);
- }, 3000);
- } else {
- done();
- }
- }
- }).then(action => {
- this.$message({
- type: 'info',
- message: 'action: ' + action
- });
- });
- }
- }
- }
- </script>
使用 HTML 片段
message
属性支持传入 HTML 片段
将dangerouslyUseHTMLString
属性设置为 true,message
就会被当作 HTML 片段处理。
- <template>
- <el-button type="text" @click="open5">点击打开 Message Box</el-button>
- </template>
- <script>
- export default {
- methods: {
- open5() {
- this.$alert('<strong>这是 <i>HTML</i> 片段</strong>', 'HTML 片段', {
- dangerouslyUseHTMLString: true
- });
- }
- }
- }
- </script>
message
属性虽然支持传入 HTML 片段,但是在网站上动态渲染任意 HTML 是非常危险的,因为容易导致 XSS 攻击。因此在 dangerouslyUseHTMLString
打开的情况下,请确保 message
的内容是可信的,永远不要将用户提交的内容赋值给 message
属性。
居中布局
内容支持居中布局
将 center
设置为 true
即可开启居中布局
- <template>
- <el-button type="text" @click="open6">点击打开 Message Box</el-button>
- </template>
- <script>
- export default {
- methods: {
- open6() {
- this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning',
- center: true
- }).then(() => {
- this.$message({
- type: 'success',
- message: '删除成功!'
- });
- }).catch(() => {
- this.$message({
- type: 'info',
- message: '已取消删除'
- });
- });
- }
- }
- }
- </script>
全局方法
如果你完整引入了 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)
单独引用
如果单独引入 MessageBox
:
- import { MessageBox } from 'element-ui';
那么对应于上述四个全局方法的调用方法依次为:MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt,调用参数与全局方法相同。
Options
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
title | MessageBox 标题 | string | — | — |
message | MessageBox 消息正文内容 | string / VNode | — | — |
dangerouslyUseHTMLString | 是否将 message 属性作为 HTML 片段处理 | boolean | — | false |
type | 消息类型,用于显示图标 | string | success / info / warning / error | — |
customClass | MessageBox 的自定义类名 | string | — | — |
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 实例 | — | — |
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 |
MessageBox 弹框的更多相关文章
- 关于ElementUI中MessageBox弹框的取消键盘触发事件(enter,esc)关闭弹窗(执行事件)的解决方法
好久没见了 在项目中遇到一个小小的需求,总结了一下! 详细我就不介绍了,相信大家用过的话,很了解.详见文档-----------> http://element-cn.eleme.io/#/zh ...
- ui-element消息类型 MessageBox 弹框 type类型
MessageBox 弹框 type字段表明消息类型,可以为success,error,info和warning
- elementUI MessageBox弹框 <el-dialog>弹框如果出现input的type属性为password。项目中用到日期组件的地方会报错
ElementUI:项目中如果用到MessageBox弹框的输入框input且type为password,以及用到<el-dialog>里面用到input且type为password.此时 ...
- 在vue项目中的main.js中直接使用element-ui中的Message 消息提示、MessageBox 弹框、Notification 通知
需求来源:向后台请求数据时后台挂掉了,后台响应就出现错误,不做处理界面就卡住了,这时需要在main.js中使用axios的响应拦截器在出现相应错误是给出提示.项目使用element-ui,就调用里面的 ...
- element-- 修改MessageBox 弹框 中确定和取消按钮顺序
需求:修改弹框中的 取消/确定按钮顺序,及头部和底部背景颜色; 原ui效果图 需求ui效果图 方法:对取消及确定按钮自定义类名,样式重写
- DevExpress MessageBox 弹出框 底层类
效果图: 前台调用: //图一的前台调用 MessageBox.Show("测试", "标题", MessageBoxButtons.OK); //图二的前台调 ...
- JS 功能弹框封装
// 功能提示弹框 function messageBox ( option ) { var html = ''; html += '<div class="message-box-h ...
- CreateProcessAsUser,C#写的windows服务弹框提示消息或者启动子进程
服务(Service)对于大家来说一定不会陌生,它是Windows 操作系统重要的组成部分.我们可以把服务想像成一种特殊的应用程序,它随系统的“开启-关闭”而“开始-停止”其工作内容,在这期间无需任何 ...
- python之tkinter使用-消息弹框
# messagebox:消息弹框 # 不断点击按钮,切换各种弹窗 import tkinter as tk from tkinter import messagebox from tk_center ...
随机推荐
- D - A or...or B Problem
题意:给定A,B,问[A,B]里取任意个数按位或,结果有多少种. 思路:这题需要找出一个分界点,即找到最高位的B是1,A是0的位置x(最低位从0开始),那么对于所有OR的结果,x处要么是1要么是0,x ...
- Vim——替换命令
原创声明:本文系博主原创文章,转载或引用请注明出处. 1. 文本替换 #替换每一行的第一个a :%s/a/b/ #全部替换 :%s/a/b/g #替换/时需要转义"\/" 2. ...
- linux抓取usb包设置usbmon
- Blend Visual studio 和Visual studio 的区别
blend for visual studio 这个并不是用于开发中写代码的,而是专门用来做WPF的界面设计的.这是为了让专业的人做专业的事,尽量让写代码的人只使用visual studio,而做界面 ...
- Array 对象-sort()
Array 对象-sort() sort方法对数组成员进行排序,默认是按照字典顺序排序.排序后,原数组将被改变. sort方法不是按照大小排序,而是按照字典顺序.也就是说,数值会被先转成字符串,再按照 ...
- Mybatis关联查询(转载)
原文地址: http://www.cnblogs.com/xiaolang8762400/p/7399892.html mybatis 提供了高级的关联查询功能,可以很方便地将数据库获取的结果集映 ...
- elasticsearch启动时遇到的错误
elasticsearch启动时遇到的错误 问题翻译过来就是:elasticsearch用户拥有的内存权限太小,至少需要262144: 解决: 切换到root用户 执行命令: sysctl -w vm ...
- BZOJ 4128: Matrix (矩阵BSGS)
类比整数的做法就行了 1A爽哉 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int M ...
- 坚果Pro2刷魔趣系统教程,刷回锤子系统教程
一.刷魔趣系统 1.高通驱动安装 https://blog.csdn.net/qq_43653944/article/details/86702169 2.刷入twrp rec https://blo ...
- 验证码生成 C#
/// <summary> /// 验证码类 /// </summary> public class Rand { #region 生成随机数字 /// <summary ...