Message 消息提示

经常使用于主动操做后的反馈提示。与 Notification 的区别是后者更多用于系统级通知的被动提醒。html

基础用法

从顶部出现,3 秒后自动消失。vue

Message 在配置上与 Notification 很是相似,因此部分 options 在此不作详尽解释,文末有 options 列表,能够结合 Notification 的文档理解它们。Element 注册了一个$message方法用于调用,Message 能够接收一个字符串或一个 VNode 做为参数,它会被显示为正文内容。element-ui

 1 <template>
 2   <el-button :plain="true" @click="open">打开消息提示</el-button>
 3   <el-button :plain="true" @click="openVn">VNode</el-button>
 4 </template>
 5 
 6 <script>
 7   export default {
 8     methods: {
 9       open() {
10         this.$message('这是一条消息提示');
11       },
12 
13       openVn() {
14         const h = this.$createElement;
15         this.$message({
16           message: h('p', null, [
17             h('span', null, '内容能够是 '),
18             h('i', { style: 'color: teal' }, 'VNode')
19           ])
20         });
21       }
22     }
23   }
24 </script>
View Code

 

不一样状态

用来显示「成功、警告、消息、错误」类的操做反馈。ide

当须要自定义更多属性时,Message 也能够接收一个对象为参数。好比,设置type字段能够定义不一样的状态,默认为info。此时正文内容以message的值传入。同时,咱们也为 Message 的各类 type 注册了方法,能够在不传入type字段的状况下像open4那样直接调用。函数

 1 <template>
 2   <el-button :plain="true" @click="open2">成功</el-button>
 3   <el-button :plain="true" @click="open3">警告</el-button>
 4   <el-button :plain="true" @click="open">消息</el-button>
 5   <el-button :plain="true" @click="open4">错误</el-button>
 6 </template>
 7 
 8 <script>
 9   export default {
10     methods: {
11       open() {
12         this.$message('这是一条消息提示');
13       },
14       open2() {
15         this.$message({
16           message: '恭喜你,这是一条成功消息',
17           type: 'success'
18         });
19       },
20 
21       open3() {
22         this.$message({
23           message: '警告哦,这是一条警告消息',
24           type: 'warning'
25         });
26       },
27 
28       open4() {
29         this.$message.error('错了哦,这是一条错误消息');
30       }
31     }
32   }
33 </script>
View Code

 

可关闭

能够添加关闭按钮。网站

默认的 Message 是不能够被人工关闭的,若是须要可手动关闭的 Message,可使用showClose字段。此外,和 Notification 同样,Message 拥有可控的duration,设置0为不会被自动关闭,默认为 3000 毫秒。ui

 1 <template>
 2   <el-button :plain="true" @click="open5">消息</el-button>
 3   <el-button :plain="true" @click="open6">成功</el-button>
 4   <el-button :plain="true" @click="open7">警告</el-button>
 5   <el-button :plain="true" @click="open8">错误</el-button>
 6 </template>
 7 
 8 <script>
 9   export default {
10     methods: {
11       open5() {
12         this.$message({
13           showClose: true,
14           message: '这是一条消息提示'
15         });
16       },
17 
18       open6() {
19         this.$message({
20           showClose: true,
21           message: '恭喜你,这是一条成功消息',
22           type: 'success'
23         });
24       },
25 
26       open7() {
27         this.$message({
28           showClose: true,
29           message: '警告哦,这是一条警告消息',
30           type: 'warning'
31         });
32       },
33 
34       open8() {
35         this.$message({
36           showClose: true,
37           message: '错了哦,这是一条错误消息',
38           type: 'error'
39         });
40       }
41     }
42   }
43 </script>
View Code

 

文字居中

使用 center 属性让文字水平居中。this

 1 <template>
 2   <el-button :plain="true" @click="openCenter">文字居中</el-button>
 3 </template>
 4 
 5 <script>
 6   export default {
 7     methods: {
 8       openCenter() {
 9         this.$message({
10           message: '居中的文字',
11           center: true
12         });
13       }
14     }
15   }
16 </script>
View Code

 

使用 HTML 片断

message 属性支持传入 HTML 片断spa

dangerouslyUseHTMLString属性设置为 true,message 就会被看成 HTML 片断处理。prototype

 1 <template>
 2   <el-button :plain="true" @click="openHTML">使用 HTML 片断</el-button>
 3 </template>
 4 
 5 <script>
 6   export default {
 7     methods: {
 8       openHTML() {
 9         this.$message({
10           dangerouslyUseHTMLString: true,
11           message: '<strong>这是 <i>HTML</i> 片断</strong>'
12         });
13       }
14     }
15   }
16 </script>
View Code

 

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
相关文章
相关标签/搜索