消息提示组件封装(以函数形式调用)

1. 封装后的效果图

在这里插入图片描述

2. 使用方法

Message({ type: 'error', text: '登录失败' }) // 错误弹框提示 文案为登录失败

3. 基础思路

1)以Message组件为基础创建虚拟dom - createVNode
2)准备一个等待渲染Message组件的容器 - container容器
3)把Message组件生成的虚拟dom渲染到准备好的容器中 - render

4. 结构设计

<template>
  <transition name="down">
    <div class="xtx-message" :style="style[type]" v-if="visible">
      <!-- 上面绑定的是样式 -->
      <i class="iconfont" :class="[icons[type]]"></i>
      <!-- 不同提示图标会变 -->
      <span class="text">{{ text }}</span>
    </div>
  </transition>
</template>
<script>
import { onMounted, ref } from 'vue'
export default {
  name: 'Message',
  props: {
    text: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      // warn 警告  error 错误  success 成功
      default: 'warn'
    }
  },
  setup () {
    // 样式集合
    const style = {
      warn: {
        color: '#E6A23C',
        backgroundColor: 'rgb(253, 246, 236)',
        borderColor: 'rgb(250, 236, 216)'
      },
      error: {
        color: '#F56C6C',
        backgroundColor: 'rgb(254, 240, 240)',
        borderColor: 'rgb(253, 226, 226)'
      },
      success: {
        color: '#67C23A',
        backgroundColor: 'rgb(240, 249, 235)',
        borderColor: 'rgb(225, 243, 216)'
      }
    }

    // 图标类名集合
    const icons = {
      warn: 'icon-warning',
      error: 'icon-shanchu',
      success: 'icon-queren2'
    }

    // 控制显隐让过渡生效
    // 虽然直接是在mounted钩子函数中做状态的修改
    // 但是模板区域的div依旧存在从不显示到显示的状态切换
    const visible = ref(false) // 隐藏
    onMounted(() => {
      visible.value = true
    })
    return { style, icons, visible }
  }
}
</script>
<style scoped lang="less">
.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;
  }
}

// .down-enter-from{}  // 元素进入到dom结构中之前要渲染的类名
// .down-enter-active{} // 元素从不显示到显示进入中状态时显示的类名  (定义给哪些属性应用过渡  过渡的总时长)
// .down-enter-to{} // 元素进入到dom结构中之后要渲染的类名

// 俩个必要条件
// 1. transtion内置组件包裹  name
// 2. 通过v-if/v-show/控制显示和隐藏

.down {
  &-enter {
    // 进入前 在正常文档流的基础上往上移动75px 并且透明度为零完全透明
    &-from {
      transform: translate3d(0, -75px, 0);
      opacity: 0;
    }
    // 进入中 对所有的可以支持过滤效果的样式都应用过渡效果 整个过渡时长是0.5s
    &-active {
      transition: all 0.5s;
    }
    // 进入后 恢复到正常状态
    &-to {
      transform: none;
      opacity: 1;
    }
  }
}
</style>

5. 逻辑代码

import MessageComponent from './index.vue'
import { createVNode, render } from 'vue'

function Message ({ type, text }) {
  // 核心逻辑
  // 1. 以Message组件为基础生成一个VNode
  // 2. 准备一个挂载我们组件的节点元素出来(body -> id Container)
  // 3. 使用render函数渲染到某个节点中
  const MVNode = createVNode(MessageComponent, { text, type })

  const container = document.createElement('div')

  document.body.appendChild(container)

  render(MVNode, container)

  // 1500之后消失
  setTimeout(() => {
    render(null, container)
  }, 1500)
}

6. 相关知识

1)createVNode 生成虚拟dom节点
2)render 把虚拟dom节点渲染到真实的dom中

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值