消息提示框组件封装

消息提示框组件封装

思路

  1. 模版:图标+文字提示

  2. 父组件传入样式类型和文字

  3. 子组件内props接受

  4. 子组件内setup内定义一个对象,包含三种情况的样式:警告,错误,成功,对象key的就是类型字符串

  5. v-show 绑定 visible 该组件默认是隐藏的,visible 默认值是false ,在生命周期钩子函数 onMounted 内 将visible 置为true

  6. 封装成一个消息提示函数方便以后使用,直接调用就行

 
<template>
   <transition name='down'>
     <div class="xtx-message" :style="style[type]" v-show='visible'>
       <!-- 上面绑定的是样式 -->
       <!-- 不同提示图标会变 -->
       <i class="iconfont" :class="[style[type].icon]"></i>
       <span class="text">{{text}}</span>
     </div>
   </transition>
 </template>
 <script>
 import { onMounted, ref } from 'vue'
 ​
 export default {
   name: 'XtxMessage',
   props: {
     text: {
       type: String,
       default: ''
     },
     type: {
       type: String,
       // warn 警告  error 错误  success 成功
       default: 'warn'
     }
   },
   setup () {
     // 定义一个对象,包含三种情况的样式,对象key就是类型字符串
     const style = {
       warn: {
         icon: 'icon-warning',
         color: '#E6A23C',
         backgroundColor: 'rgb(253, 246, 236)',
         borderColor: 'rgb(250, 236, 216)'
       },
       error: {
         icon: 'icon-shanchu',
         color: '#F56C6C',
         backgroundColor: 'rgb(254, 240, 240)',
         borderColor: 'rgb(253, 226, 226)'
       },
       success: {
         icon: 'icon-queren2',
         color: '#67C23A',
         backgroundColor: 'rgb(240, 249, 235)',
         borderColor: 'rgb(225, 243, 216)'
       }
     }
     // 控制动画
     const visible = ref(false)
 ​
     onMounted(() => {
       visible.value = true
     })
 ​
     return { style, visible }
   }
 }
 </script>
 <style scoped lang="less">
 .down {
   &-enter {
     &-from {
       transform: translate3d(0, -75px, 0);
       opacity: 0;
     }
     &-active {
       transition: all 0.5s;
     }
     &-to {
       transform: none;
       opacity: 1;
     }
   }
 }
 .xtx-message {
   width: 300px;
   height: 50px;
   position: fixed;
   z-index: 9999;
   left: 50%;
   margin-left: -150px;
   top: 25px;
   line-height: 50px;
   padding: 0 25px;
   border: 1px solid #e4e4e4;
   background: #f5f5f5;
   color: #999;
   border-radius: 4px;
   i {
     margin-right: 4px;
     vertical-align: middle;
   }
   .text {
     vertical-align: middle;
   }
 }
 </style>
 ​

将组件封装成函数的步骤

  1. 导入组件

  2. 使用原生的方法 document.createElement('div') 创建一个 div 容器

  3. 使用原生的方法 setAttribute 给 div 设置一个类名

  4. 使用原生的方法添加这个div DOM元素 document.body.appendChild(div)

  5. 导出一个函数,接受一个对象作为参数

  6. 函数体内,使用 createVNode 方法创建一个虚拟 DOM

  7. 使用 render 函数将这个虚拟 DOM 渲染到页面上

  8. 优化:当函数调用时,消息提示框渲染在页面上,但是不能让一直在页面上显示,使用定时器做防抖,3秒后销毁组件,定时器内再次调用render函数,div容器内渲染内容为null,并在每次触发定时任务时,先清除定时器

 // 封装一个消息提示函数
 import XtxMessage from './xtx-message.vue'
 import { createVNode, render } from 'vue'
 ​
 // 提前准备好dom元素(动态创建一个然后添加到页面中)
 const div = document.createElement('div')
 div.setAttribute('class', 'xtx-message-container')
 document.body.appendChild(div)
 let timer = null
 ​
 // options = {type: 'success', info: '登录成功'}
 export default ({ type, text }) => {
   // 在HTML页面中渲染一个组件xtx-message即可
   // 1、创建一个虚拟节点(基于组件)
   // 参数一:可以传入一个组件对象
   // 参数二:提供给组件的属性
   const vnode = createVNode(XtxMessage, { type, text })
   // 2、把虚拟节点渲染到页面中
   // 参数一:表示被渲染的虚拟节点
   // 参数二:表示渲染的目标位置(DOM元素)
   render(vnode, div)
   // 3、要求3秒后,销毁提示信息
   // 保证定时任务只能有一个存在
   clearTimeout(timer)
   timer = setTimeout(() => {
     // 销毁提示信息
     render(null, div)
   }, 3000)
 }
 ​
 // import Message from './Message.js'
 ​

使用

  1. 导入

  2. Message函数调用,并传入对象作为参数

 
import Message from '@/components/library/Message.js'
 Message({ type :'success' , text:'成功' })

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值