美文网首页
封装Message弹框组件

封装Message弹框组件

作者: Jycoding | 来源:发表于2022-05-03 18:04 被阅读0次

Message.js文件

// 提供一个能够显示xtx-message组件的函数

// 这个函数将来:导入直接使用,也可以挂载在vue实例原型上

// import Message from 'Message.js' 使用 Message({type:'error',text:'提示文字'})

// this.$message({type:'error',text:'提示文字'})

import { createVNode, render } from 'vue'

import XtxMessage from './xtx-message.vue'

// DOM容器

const div = document.createElement('div')

div.setAttribute('class', 'xtx-msssage-container')

document.body.appendChild(div)

// 定时器标识

let timer = null

export default ({ type, text }) => {

  // 渲染组件

  // 1. 导入消息提示组件

  // 2. 将消息提示组件编译为虚拟节点(dom节点)

  // createVNode(组件,属性对象(props))

  const vnode = createVNode(XtxMessage, { type, text })

  // 3. 准备一个装载消息提示组件的DOM容器

  // 4. 将虚拟节点渲染再容器中

  // render(虚拟节点,DOM容器)

  render(vnode, div)

  // 5. 3s后销毁组件

  clearTimeout(timer)

  timer = setTimeout(() => {

    render(null, div)

  }, 3000)

}

<template>

  <Transition name="down">

    <div class="xtx-message" :style="style[type]" v-show="visible">

      <!-- 上面绑定的是样式 -->

      <!-- 不同提示图标会变 :class="{'icon-warning':true}" :class="['icon-warning']" -->

      <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: {

    type: {

      type: String,

      default: 'warn'

    },

    text: {

      type: String,

      default: ''

    }

  },

  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>

相关文章