vue3 ts 手动封装message消息组件

在这里插入图片描述

1. 组件封装

在这里插入图片描述
message.vue

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

defineProps({
  type: {
    type: String as PropType<'success' | 'error' | 'warning' | 'info'>,
    default: 'success'
  },
  message: {
    type: String,
    default: '恭喜你,这是一条消息'
  }
})

const isShow = ref(false)
onMounted(() => {
  isShow.value = true
})

// 定义一个对象,包含三种情况的样式,对象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)'
  },
  info: {
    icon: 'icon-info',
    color: '#909399',
    backgroundColor: '#edf2fc',
    borderColor: '#ebeef5'
  }
}
</script>

<template>
  <Transition name="down">
    <div class="gl-message" v-show="isShow" :style="style[type]">
      <i class="iconfont" :class="style[type].icon"></i>
      <span class="text">{{ message }}</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;
    }
  }
}
.gl-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>

index.ts

import { h, render, ref } from 'vue'
import GlMessage from './message.vue'
type Props = {
  type: 'success' | 'error' | 'warning ' | 'info'
  message: string
  duration?: number
}
const div = document.createElement('div')
div.setAttribute('class', 'gl-message')
document.body.appendChild(div)
// 定时器
let timer = ref<any>(null)
export default function Message({ type, message, duration = 3000 }: Props) {
  const vNode = h(GlMessage, { type, message })
  render(vNode, div)
  timer && clearTimeout(timer.value)
  timer.value = setTimeout(() => {
    render(null, div)
  }, duration)
}

Message.sussess = (message: string, duration?: number) => {
  Message({ type: 'success', message, duration })
}
Message.error = (message: string, duration?: number) => {
  Message({ type: 'error', message, duration })
}
Message.warning = (message: string, duration?: number) => {
  Message({ type: 'warning', message, duration })
}
Message.info = (message: string, duration?: number) => {
  Message({ type: 'info', message, duration })
}

使用方式
- 引入 import Message from '@/packages/library/message'
- 两种使用方式
1.  Message({ type: 'warning', message: '登录失败' })
2.  Message.warning('登录失败')
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值