常用于主动操作后的反馈提示。与 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 消息提示的更多相关文章

  1. element组件dialog关闭时Message消息提示抖动问题

    在页面内容较多,出现滚动条时使用element组件里的dialog组件,当关闭dialog组件的同时弹出Message消息提示时,Message会抖动一下. 在页面有滚动条的情况先打开dialog时, ...

  2. 在vue项目中的main.js中直接使用element-ui中的Message 消息提示、MessageBox 弹框、Notification 通知

    需求来源:向后台请求数据时后台挂掉了,后台响应就出现错误,不做处理界面就卡住了,这时需要在main.js中使用axios的响应拦截器在出现相应错误是给出提示.项目使用element-ui,就调用里面的 ...

  3. element——message消息提示

    官方文档:http://element-cn.eleme.io/#/zh-CN/component/message 简单的用法,一句代码搞定.类型有success/warning/info/error ...

  4. 一个简单的消息提示jquery插件

    最近在工作中写了一个jquery插件,效果如下: 就是一个简单的提示消息的一个东西,支持最大化.最小化.关闭.自定义速度.自定义点击事件,数据有ajax请求和本地数据两种形式.还有不完善的地方,只做了 ...

  5. ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(五) 之 加好友,加群流程,消息管理和即时消息提示的实现

    前言 前前一篇留了个小问题,在上一篇中忘了写了,就是关于LayIM已经封装好的上传文件或者图片的问题.对接好接口之后,如果上传速度慢,界面就会出现假死情况,虽然文件正在上传.于是我就简单做了个图标替代 ...

  6. Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog)

    Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog) Android第三方开源对话消息提示框:SweetAlertDialog(sweet- ...

  7. 10. Android框架和工具之 AppMsg(消息提示)

    1. AppMsg 优雅的弹出类似Toast的消息提示,支持3种状态Alert(警告),Confirm(确认)以及Info(消息).        2. AppMsg使用: (1)AppMsg下载地址 ...

  8. 精美舒适的对话消息提示框--第三方开源--SweetAlertDialog

    SweetAlertDialog(sweet-alert-dialog)是一个套制作精美.动画效果出色生动的Android对话.消息提示框 SweetAlertDialog(sweet-alert-d ...

  9. WndProc Message消息解释

    public class WindowsMessage { public const int WM_NULL = 0x0000; // public const int WM_CREATE = 0x0 ...

随机推荐

  1. odoo xml中添加数据的数字代表含义

    参考原文:https://alanhou.org/odoo12-import-export-data/ <?xml version="1.0"?> <odoo n ...

  2. Java 基本的数据类型(8种)

    1.Java 基本的数据类型(8种) 整型:byte .short .int .long 浮点型:float .double 字符型:char 布尔型:boolean

  3. spket IDE插件更新地址

    http://www.agpad.com/update spket  IDE插件更新地址

  4. DBUtils封装数据库返回对象的各种方法

    ①ArrayHandler:     将查询结果的第一行数据,保存到Object数组中       ②ArrayListHandler     将查询的结果,每一行先封装到Object数组中,然后将数 ...

  5. 8.7.ZooKeeper Watcher监听

    1.ZooKeeper Watcher ZooKeeper 提供了分布式数据发布/订阅功能,一个典型的发布/订阅模型系统定义了一种一对多的订阅关系,能让多个订阅者同时监听某一个主题对象, 当这个主题对 ...

  6. Cobbler自动化装机脚本

    #!/bin/bash ens33_ip=192.168.1.3 ens33_gateway=192.168.1.1 ens37_ip=192.168.207.2 dhcp_wd=192.168.20 ...

  7. mysqltuner对数据库的优化

    主要用于对mysql配置及my.cnf配置检查,提供详细信息,为进一步优化mysql做参考. 下载地址: (1)http://mysqltuner.com/ (2)脚本获取# wget -c http ...

  8. TXNLP 09-17

    上节课讲了一些算法的复杂度,都比较简单,我就没有单独截图.1 n n^2 nlogn logn...等等 其实一些排序问题也比较简单.还是给大家列举一下. 归并排序: 主定理定理..吐血 算法复杂度相 ...

  9. Web前端开发——HTML基本标签

    标题h1-h6 只有h1到h6,没有h7. 一个页面建议只有一个h1 普通文字大小大约介于h3~h4 段落p 段内换行br 段内分组span 预留格式pre 水平线hr

  10. 《剑指offer》算法题第七天

    今日题目: 复杂链表的复制 二叉搜索树与双向链表 序列化二叉树 字符串的排序 1.复杂链表的复制 题目描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向 ...