• 封装简单版消息提示组件


    src/components/XtxMessage/type.d.ts

    1. export type MessageType = 'success' | 'error' | 'warning'
    2. export type Message = {
    3. type: MessageType
    4. text: string
    5. duration?: number
    6. }

    src/components/XtxMessage/index.ts

    1. // 导入XtxMessage.vue组件
    2. // 通过代码的方式去渲染它
    3. import { h, render } from 'vue'
    4. import { Message } from './type'
    5. import XtxMessage from './XtxMessage.vue'
    6. // 创建一个dom容器
    7. // 把这个容器添加在body上
    8. const div = document.createElement('div')
    9. div.className = "xtx-message-container"
    10. document.body.appendChild(div)
    11. // 调用这个函数
    12. let timer = -1
    13. export default function Message(obj: Message){
    14. const vNode = h(XtxMessage, { type: obj.type, text: obj.text})
    15. // 把虚拟dom放入上面定义的容器中
    16. render(vNode, div)
    17. clearTimeout(timer)
    18. // 开启定时器,删除
    19. timer = window.setTimeout(() => {
    20. // 从dom上删除
    21. render(null, div)
    22. }, obj.duration||2000)
    23. }
    24. Message.success = (value: string) => {
    25. Message({ type: "success", text: value, duration: 1500})
    26. }
    27. Message.error = (value: string) => {
    28. Message({ type: "error", text: value, duration: 1500})
    29. }
    30. Message.warning = (value: string) => {
    31. Message({ type: "warning", text: value, duration: 1500})
    32. }

    此时

    src/components/XtxMessage/XtxMessage.vue

    1. <template>
    2. <Transition name="down">
    3. <div class="xtx-message" :style="style[type]" v-show="isShow">
    4. <i class="iconfont" :class="style[type].icon">i>
    5. <span class="text">{{text}}span>
    6. div>
    7. Transition>
    8. template>
    9. <style scoped lang="less">
    10. .down-enter-from {
    11. transform: translateY(-70px);
    12. }
    13. .down-enter-active {
    14. transition: transform 0.5s;
    15. }
    16. .xtx-message {
    17. width: 300px;
    18. height: 50px;
    19. position: fixed;
    20. z-index: 9999;
    21. left: 50%;
    22. margin-left: -150px;
    23. top: 25px;
    24. line-height: 50px;
    25. padding: 0 25px;
    26. border: 1px solid #e4e4e4;
    27. background: #f5f5f5;
    28. color: #999;
    29. border-radius: 4px;
    30. i {
    31. margin-right: 4px;
    32. vertical-align: middle;
    33. }
    34. .text {
    35. vertical-align: middle;
    36. }
    37. }
    38. style>

    注解:

    在vue中,显示隐藏,创建移除,一个元素或者一个组件的时候,可以通过transition实现动画。

     

    • 进入(显示,创建)
    • v-enter-from 进入前
      • v-enter-to 进入后
      • v-enter-active 进入中
    • 离开(隐藏,移除)
    • v-leave-from 进入前
      • v-leave-active 进入中
      • v-leave-to 进入后

    多个transition使用不同动画,可以添加nam属性,name属性的值替换v即可。

    使用时,就直接导入 Message.      就会有提示

  • 相关阅读:
    基于Spring实现策略模式
    Ubuntu安装桌面环境以及远程桌面连接
    Linux--信号携带消息
    使用client-go实现自定义控制器
    花咲の姫君(異時層ツキハ) / 花咲(异时层妖刀)
    Pycharm 日常方便工具和快捷键
    SpringBoot-42-注册Web原生组件
    第51章 Bootstrap-Fileinput深入理解
    cubeIDE开发,结合图片取模工具,stm32程序在LCD显示图片
    k3s+traefik+cert-manager+letsencrypt实现web服务全https
  • 原文地址:https://blog.csdn.net/LJM51200/article/details/126045941