vue3 -- 封装消息提示方法

357 阅读1分钟

在这里插入图片描述

vue3 -- 封装消息提示方法

背景

在实际开发中,为了更好地用户体验,我们会在用户执行了某些操作之后,予以反馈,告诉用户操作成功还是失败,因此需要一个消息提示反馈,这样的提示组件并不会像别的页面组件一样直接呈现给用户,而是通过某些操作来触发消息提示,因此我们会把他封装成全局方法

落地代码

在 components 文件夹创建 my-message.vue 文件

props 中从外面传入两个参数

  1. text 参数:消息提示信息文本
  2. type 参数:消息类型 ,这里只封装了 三种状态(warn 警告 error 错误 success 成功)
<template>
  <!-- 显示时的动画,滑动 -->
  <transition name="down">
    <div class="my-message" :style="style[type]" v-show="isShow">
      <!-- 上面绑定的是样式 -->
      <!-- 不同提示图标会变 -->
      <!-- i标签就是消息提示内容前面的小图标 -->
      <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 isShow = ref(false)
    onMounted(() => {
      isShow.value = true
    })
    return { style, isShow }
  }
}
</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;
    }
  }
}
.my-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>

在相同目录下 创建 Message.js 文件

import { createVNode, render } from 'vue'
import MyMessage from './my-message.vue'
// 创建元素用来挂载虚拟 DOM 
const div = document.createElement('div')
div.setAttribute('class', 'xtx-message-contsiner')
document.body.appendChild(div)

export default ({ text, type }) => {
  let timer = null
  // 创建虚拟 DOM
  const VNode = createVNode(MyMessage, { text, type })
  // 通过 render 函数挂载到页面中
  render(VNode, div)
  // 添加定时器,放置触发多次
  clearTimeout(timer)
  timer = setTimeout(() => {
    render(null, div)
  }, 3000)
}

此时就可以直接使用 在需要使用的组件中引入 import Message from './Message.js' 直接调用 Message({text: '提示文本', type: '提示类型'}) 即可


如果想像 vue2 中那样使用 this.$message 则还西药进行下面的操作

在 components 文件夹中的 index.js 文件中注册到全局

import Message from './Message.js'

export default {
    install(app){
        app.config.globalProperties.$message = Message
    }
}

在组件使用

import { getCurrentInstance } from 'vue'

export default {
    setup() {
        const instance = getCurrentInstance()
        // 使用
        // instance.proxy.$message({text: '提示文本', type: '提示类型'})
    }
}