Message 消息提示
常用于主动操作后的反馈提示。与 Notification 的区别是后者更多用于系统级通知的被动提醒。
基础用法
从顶部出现,3 秒后自动消失。
Message 在配置上与 Notification 非常类似,所以部分 options 在此不做详尽解释,文末有 options 列表,可以结合 Notification 的文档理解它们。Element 注册了一个$message方法用于调用,Message 可以接收一个字符串或一个 VNode 作为参数,它会被显示为正文内容。
<template>
<el-button :plain="true" @click="open">打开消息提示</el-button>
<el-button :plain="true" @click="openVn">VNode</el-button>
</template> <script>
export default {
methods: {
open() {
this.$message('这是一条消息提示');
}, openVn() {
const h = this.$createElement;
this.$message({
message: h('p', null, [
h('span', null, '内容可以是 '),
h('i', { style: 'color: teal' }, 'VNode')
])
});
}
}
}
</script>
不同状态
用来显示「成功、警告、消息、错误」类的操作反馈。
当需要自定义更多属性时,Message 也可以接收一个对象为参数。比如,设置type字段可以定义不同的状态,默认为info。此时正文内容以message的值传入。同时,我们也为 Message 的各种 type 注册了方法,可以在不传入type字段的情况下像open4那样直接调用。
<template>
<el-button :plain="true" @click="open2">成功</el-button>
<el-button :plain="true" @click="open3">警告</el-button>
<el-button :plain="true" @click="open">消息</el-button>
<el-button :plain="true" @click="open4">错误</el-button>
</template> <script>
export default {
methods: {
open() {
this.$message('这是一条消息提示');
},
open2() {
this.$message({
message: '恭喜你,这是一条成功消息',
type: 'success'
});
}, open3() {
this.$message({
message: '警告哦,这是一条警告消息',
type: 'warning'
});
}, open4() {
this.$message.error('错了哦,这是一条错误消息');
}
}
}
</script>
可关闭
可以添加关闭按钮。
默认的 Message 是不可以被人工关闭的,如果需要可手动关闭的 Message,可以使用showClose字段。此外,和 Notification 一样,Message 拥有可控的duration,设置0为不会被自动关闭,默认为 3000 毫秒。
<template>
<el-button :plain="true" @click="open5">消息</el-button>
<el-button :plain="true" @click="open6">成功</el-button>
<el-button :plain="true" @click="open7">警告</el-button>
<el-button :plain="true" @click="open8">错误</el-button>
</template> <script>
export default {
methods: {
open5() {
this.$message({
showClose: true,
message: '这是一条消息提示'
});
}, open6() {
this.$message({
showClose: true,
message: '恭喜你,这是一条成功消息',
type: 'success'
});
}, open7() {
this.$message({
showClose: true,
message: '警告哦,这是一条警告消息',
type: 'warning'
});
}, open8() {
this.$message({
showClose: true,
message: '错了哦,这是一条错误消息',
type: 'error'
});
}
}
}
</script>
文字居中
使用 center 属性让文字水平居中。
<template>
<el-button :plain="true" @click="openCenter">文字居中</el-button>
</template> <script>
export default {
methods: {
openCenter() {
this.$message({
message: '居中的文字',
center: true
});
}
}
}
</script>
使用 HTML 片段
message 属性支持传入 HTML 片段
将dangerouslyUseHTMLString属性设置为 true,message 就会被当作 HTML 片段处理。
<template>
<el-button :plain="true" @click="openHTML">使用 HTML 片段</el-button>
</template> <script>
export default {
methods: {
openHTML() {
this.$message({
dangerouslyUseHTMLString: true,
message: '<strong>这是 <i>HTML</i> 片段</strong>'
});
}
}
}
</script>
message 属性虽然支持传入 HTML 片段,但是在网站上动态渲染任意 HTML 是非常危险的,因为容易导致 XSS 攻击。因此在 dangerouslyUseHTMLString 打开的情况下,请确保 message 的内容是可信的,永远不要将用户提交的内容赋值给 message属性。
全局方法
Element 为 Vue.prototype 添加了全局方法 $message。因此在 vue instance 中可以采用本页面中的方式调用 Message。
单独引用
单独引入 Message:
import { Message } from 'element-ui';
此时调用方法为 Message(options)。我们也为每个 type 定义了各自的方法,如 Message.success(options)。并且可以调用 Message.closeAll() 手动关闭所有实例。
Options
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| message | 消息文字 | string / VNode | — | — |
| type | 主题 | string | success/warning/info/error | info |
| iconClass | 自定义图标的类名,会覆盖 type |
string | — | — |
| dangerouslyUseHTMLString | 是否将 message 属性作为 HTML 片段处理 | boolean | — | false |
| customClass | 自定义类名 | string | — | — |
| duration | 显示时间, 毫秒。设为 0 则不会自动关闭 | number | — | 3000 |
| showClose | 是否显示关闭按钮 | boolean | — | false |
| center | 文字是否居中 | boolean | — | false |
| onClose | 关闭时的回调函数, 参数为被关闭的 message 实例 | function | — | — |
方法
调用 Message 或 this.$message 会返回当前 Message 的实例。如果需要手动关闭实例,可以调用它的 close 方法。
| 方法名 | 说明 |
|---|---|
| close | 关闭当前的 Message |
Message 消息提示的更多相关文章
- element组件dialog关闭时Message消息提示抖动问题
在页面内容较多,出现滚动条时使用element组件里的dialog组件,当关闭dialog组件的同时弹出Message消息提示时,Message会抖动一下. 在页面有滚动条的情况先打开dialog时, ...
- 在vue项目中的main.js中直接使用element-ui中的Message 消息提示、MessageBox 弹框、Notification 通知
需求来源:向后台请求数据时后台挂掉了,后台响应就出现错误,不做处理界面就卡住了,这时需要在main.js中使用axios的响应拦截器在出现相应错误是给出提示.项目使用element-ui,就调用里面的 ...
- element——message消息提示
官方文档:http://element-cn.eleme.io/#/zh-CN/component/message 简单的用法,一句代码搞定.类型有success/warning/info/error ...
- 一个简单的消息提示jquery插件
最近在工作中写了一个jquery插件,效果如下: 就是一个简单的提示消息的一个东西,支持最大化.最小化.关闭.自定义速度.自定义点击事件,数据有ajax请求和本地数据两种形式.还有不完善的地方,只做了 ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(五) 之 加好友,加群流程,消息管理和即时消息提示的实现
前言 前前一篇留了个小问题,在上一篇中忘了写了,就是关于LayIM已经封装好的上传文件或者图片的问题.对接好接口之后,如果上传速度慢,界面就会出现假死情况,虽然文件正在上传.于是我就简单做了个图标替代 ...
- Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog)
Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog) Android第三方开源对话消息提示框:SweetAlertDialog(sweet- ...
- 10. Android框架和工具之 AppMsg(消息提示)
1. AppMsg 优雅的弹出类似Toast的消息提示,支持3种状态Alert(警告),Confirm(确认)以及Info(消息). 2. AppMsg使用: (1)AppMsg下载地址 ...
- 精美舒适的对话消息提示框--第三方开源--SweetAlertDialog
SweetAlertDialog(sweet-alert-dialog)是一个套制作精美.动画效果出色生动的Android对话.消息提示框 SweetAlertDialog(sweet-alert-d ...
- WndProc Message消息解释
public class WindowsMessage { public const int WM_NULL = 0x0000; // public const int WM_CREATE = 0x0 ...
随机推荐
- Thinkphp清除缓存文件
Thinkphp的缓存在开发中是非常烦人的,因为有的时候因为缓存的问题而出现的错误是非常难找的.删除缓存更是麻烦,还要去文件夹下删除.如果是linux开发服务器的话还要登陆服务器进行删除.所以这个时候 ...
- GitChat·人工智能 | 除了深度学习,机器翻译还需要啥?
本文开始要写作的时候,翻译圈里出了一个“爆炸性”的事件.6月27日下午,一个同传译员在朋友圈里爆料:某AI公司请这位译员去“扮演”机器同传,制造人工智能取代人工同传的“震撼”效果. 这个事件瞬间在译员 ...
- solr 查询解析器
定义 查询解析器用于将查询语句(q参数)解析成搜索语法. 默认解析器:lucene Solr在查询的时候,用到了QueryParser对用户输入做解析,solr默认使用的解析器是lucene,被称之为 ...
- 网卡绑定(bonding)
就是将多块网卡绑定同一IP地址对外提供服务,可以实现高 可用或者负载均衡.当然,直接给两块网卡设置同一IP地址 是不可能的.通过bonding,虚拟一块网卡对外提供连接, 物理网卡的被修改为相同的MA ...
- nginx第七天
nginx的proxy_buffering和proxy_cache 两个都是nginx代理中内存设置相关的参数. proxy_buffering设置 proxy_buffering主要是实现被代理服务 ...
- php类相关知识----类常量,静态变量
类常量 <?php class myuser { ;//定义的常量不带$符号,常量之前没有访问修饰符,常量之前没有修饰符号 public function monolog() { echo &q ...
- SQL CASE Syntax
CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_list] ... [ELSE ...
- PC监听鼠标和键盘事件,定时无响应退出
直接上代码: window.onload = function () { initScreenSaver(); } //0912 add function ScreenSaver(settings){ ...
- 把 textbox 遍历赋值为空
foreach (Control aa in this.Form.Controls){ if (aa.GetType().ToString() == "System.Web.UI.We ...
- 与word、excel交互问题总结
不同版本的Office对应的型号不同,往往问题出现在注册表中有多个版本,所以程序运行经常提示错误. 1.找不到引用microsoft.office.core解决办法 (引用中有感叹号,说明引用不成功) ...