当前位置: 首页 > news >正文

ElementUI浅尝辄止24:Message 消息提示

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

1.如何使用?

Message 在配置上与 Notification 非常类似,所以部分 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>

2.不同状态

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

/*当需要自定义更多属性时,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="open1">消息</el-button><el-button :plain="true" @click="open4">错误</el-button>
</template><script>export default {methods: {open1() {this.$message('这是一条消息提示');},open2() {this.$message({message: '恭喜你,这是一条成功消息',type: 'success'});},open3() {this.$message({message: '警告哦,这是一条警告消息',type: 'warning'});},open4() {this.$message.error('错了哦,这是一条错误消息');}}}
</script>

3.可关闭

可以添加关闭按钮。

//默认的 Message 是不可以被人工关闭的,如果需要可手动关闭的 Message,可以使用showClose字段。此外,和 Notification 一样,Message 拥有可控的duration,设置0为不会被自动关闭,默认为 3000 毫秒。<template><el-button :plain="true" @click="open1">消息</el-button><el-button :plain="true" @click="open2">成功</el-button><el-button :plain="true" @click="open3">警告</el-button><el-button :plain="true" @click="open4">错误</el-button>
</template><script>export default {methods: {open1() {this.$message({showClose: true,message: '这是一条消息提示'});},open2() {this.$message({showClose: true,message: '恭喜你,这是一条成功消息',type: 'success'});},open3() {this.$message({showClose: true,message: '警告哦,这是一条警告消息',type: 'warning'});},open4() {this.$message({showClose: true,message: '错了哦,这是一条错误消息',type: 'error'});}}}
</script>

4.文字居中

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

<template><el-button :plain="true" @click="openCenter">文字居中</el-button>
</template><script>export default {methods: {openCenter() {this.$message({message: '居中的文字',center: true});}}}
</script>

5.使用 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 属性。 

6.全局方法

Element 为 Vue.prototype 添加了全局方法 $message。因此在 vue instance 中可以采用本页面中的方式调用 Message

7.单独引用

单独引入 Message

import { Message } from 'element-ui';

此时调用方法为 Message(options)。我们也为每个 type 定义了各自的方法,如 Message.success(options)。并且可以调用 Message.closeAll() 手动关闭所有实例。

http://www.lryc.cn/news/164216.html

相关文章:

  • 让照片动起来的软件,轻松制作照片动效
  • 【图解RabbitMQ-7】图解RabbitMQ五种队列模型(简单模型、工作模型、发布订阅模型、路由模型、主题模型)及代码实现
  • Linux命令200例:write用于向特定用户或特定终端发送信息
  • javaee spring整合mybatis spring帮我们创建dao层
  • 修改Tomcat的默认端口号
  • Open3D Ransac拟合空间直线(python详细过程版)
  • 题目:2729.判断一个数是否迷人
  • 微服务模式:服务发现模式
  • 9.4 数据库 TCP
  • 普通用户使用spark的client无法更新Ranger策略
  • Git超详细教程
  • C++ 回调函数
  • xilinx FPGA IOB约束使用以及注意事项
  • 如何统计iOS产品不同渠道的下载量?
  • 大模型学习
  • Redis原理:IntSet
  • 【已解决】Splunk 8.2.X 升级ES 后红色报警
  • 香橙派使用外设驱动库wiringOP 配合定时器来驱动舵机
  • C++学习笔记--函数重载(2)
  • 代码随想录算法训练营Day56 || ● 583. 两个字符串的删除操作 ● 72. 编辑距离
  • chrome_elf.dll丢失怎么办?修复chrome_elf.dll文件的方法
  • 代码随想录32|738.单调递增的数字,968.监控二叉树,56. 合并区间
  • BIO NIO AIO演变
  • JVM GC垃圾回收
  • 【数据结构】队列知识点总结--定义;基本操作;队列的顺序实现;链式存储;双端队列;循环队列
  • 嵌入式学习之链表
  • 静态代理和动态代理笔记
  • [SM6225][Android13]user版本默认允许root和remount
  • pyinstaller打包exe,使用wexpect的问题
  • OpenCV(三十三):计算轮廓面积与轮廓长度