vue3 加 TypeScript 消息提示组件封装

消息提示组件-基础封装

基本结构

<script lang="ts" setup name="XtxMessage">

defineProps<{type:'success' | 'error' | 'warning'}>()

// 定义一个对象,包含三种情况的样式,对象key就是类型字符串
const style = {
  warning: {
    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)',
  },
}
</script>

<template>
  <div class="xtx-message" :style="style[type]">
    <i class="iconfont" :class="style[type].icon"></i>
    <span class="text"><slot></slot></span>
  </div>
</template>

<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;
  }
}
</style>

全局注册

import XtxMessage from '@/components/XtxMessage/XtxMessage.vue'
app.component('XtxMessage', XtxMessage)
// 类型声明
declare module 'vue' {
  export interface GlobalComponents {
    XtxMessage: typeof XtxMessage
  }
}

在App.vue中渲染

<template>
  <router-view />
  <XtxMessage type="error">我是提示消息</XtxMessage>
</template>

消息提示组件-函数封装

目前,message组件还不够好用,主要问题有2个

  1. 使用麻烦
  2. 样式会受到父元素的影响

(1)新建文件src/components/XtxMessage/index.ts

 // 导入XtxMessage.vue组件
// 通过代码的方式去渲染它

import { h, render } from 'vue'
import { Message } from './type'

import XtxMessage from './XtxMessage.vue'

// 创建一个dom容器
// 把这个容器添加在body上
const div = document.createElement('div')
div.className = "xtx-message-container"
document.body.appendChild(div)


// 调用这个函数
let timer = -1
export default function Message(obj: Message){
  const vNode = h(XtxMessage, { type: obj.type, text: obj.text})
  // 把虚拟dom放入上面定义的容器中
  render(vNode, div)
  // 开启延时器
  clearTimeout(timer)
  timer = window.setTimeout(()=>{
    render(null,div)
  },obj.duration||2000)
}

Message.success=(value:string)=>{
  Message({type:'success',text:value})
}

Message.error=(value:string)=>{
  Message({type:'error',text:value})
}

Message.warning=(value:string)=>{
  Message({type:'warning',text:value})
}

调用

<script setup lang="ts">
import Message from "@/components/XtxMessage/index";
Message({
  type: "success",
  text: "千玺",
});
Message({
  type:'error',
  text:'王源'
})
</script>

<template>
  <div>
    <!-- 消息提醒 -->
    <!-- <XtxMessage text="qianx" type="success"></XtxMessage> -->
  </div>
</template>

<style lang="less" scoped></style>

消息提示组件-动画效果优化
具体组件代码

<script lang="ts" setup name="XtxMessage">
import { onMounted, ref } from 'vue';

defineProps<{
type:'success' | 'error' | 'warning'
text:string
}>()

// 定义一个对象,包含三种情况的样式,对象key就是类型字符串
const style = {
  warning: {
    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)
// dom渲染完成,显示
onMounted(()=>{
  isShow.value=true
})
</script>

<template>
<!-- Transition动画效果 -->
<Transition name="down">
  <div class="xtx-message" :style="style[type]" v-show="isShow">
    <i class="iconfont" :class="style[type].icon"></i>
    <span class="text">{{text}}</span>
  </div>
  </Transition>
</template>

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

版权声明:本文为m0_68331234原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。