vue3 实现最轻量级的消息提示组件,可注册为全局消息提示组件

 代码可以直接粘贴

Message.vue

<template>
  <Transition name="slide-fade">
    <div class="my-message" :style="style[type]" v-show='message.isShow'>
      <!-- 上面绑定的是样式 -->
      <!-- 不同提示图标会变 -->
      <!-- <i class="iconfont" :class="[style[type].icon]"></i> -->
      <span class="text">{{ text }}</span>
    </div>
  </Transition>
</template>
<script>
import { onMounted, reactive } from 'vue'
export default {
  name: 'myMessage',
  props: {
    text: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      // warn 警告  error 错误  success 成功
      default: 'warn'
    }, duration: {
      type: Number,
      default: 3000
    }
  },
  setup(props) {
    // 定义一个对象,包含三种情况的样式,对象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 message = reactive({
      // 列数
      isShow: false
    })
    // 控制动画
    // const isShow = ref(false)
    // 组件模板渲染成功后触发
    onMounted(() => {
      // isShow.value = true;
      message.isShow = true
      setTimeout(() => {
        message.isShow = false
      }, props.duration)
    })
    return { style, message }
  }
}
</script>
<style lang="less">
.slide-fade-enter-active {
  transition: all 0.3s ease-out;
}

.slide-fade-leave-active {
  transition: all 0.8s cubic-bezier(1, 0.5, 0.8, 1);
}

.slide-fade-enter-from,
.slide-fade-leave-to {
  transform: translateX(20px);
  opacity: 0;
}


.my-message-container {
  width: 100%;
  display: flex;
  /* 设置为弹性容器 */
  flex-direction: column;
  /* 纵向排列子元素 */
  align-items: stretch;
  /* 子元素宽度自适应 */
  position: relative;
  /* 父元素设置为相对定位 */
  z-index: 9999;
}

.my-message {
  margin: 5px auto;
  width: 70%;
  line-height: 30px;
  padding: 0 25px;
  border: 1px solid #e4e4e4;
  background: #f5f5f5;
  color: #999;
  border-radius: 4px;

  i {
    margin-right: 4px;
    vertical-align: middle;
  }

  .text {
    vertical-align: auto;
    text-align: center;
  }
}</style>

index,js

import { createVNode, render } from 'vue'
import myMessage from './message.vue'
// 动态创建一个DOM容器

export default ({ text, type, duration = 3000 }) => {
    let timer = null
    //createVNode 用于创建一个虚拟节点
    // 参数1 支持组件
    // 参数2 表示传递给组件的选项

    const div = document.createElement('div')
    
    // const div = document.body
    div.setAttribute('class', 'my-message-container')
    
    // console.log(myMessage);
    const vnode = createVNode(myMessage, { text, type })
    //把虚拟的节点渲染到页面的DOM中即可
    // render函数的参数
    //参数一:表示表示需要渲染的内容(虚拟节点)
    // 参数二:表示渲染的目标位置 (DOM元素)
    render(vnode, div)


    const a= document.body.appendChild(div)
    timer = setTimeout(() =>
        setTimeout(() => {
            // render(null, div)
            console.log(a);
            document.body.removeChild(a);
        }, duration * 1.5)
    ); // 显示3秒钟后自动关闭

    console.log(timer);
}

 上面两个就是一个完整的组件了,可以用个文件夹,Message 

如何引用 main,js

import { createApp } from 'vue'
import App from './App.vue'

import Message from './components/Message'
const app = createApp(App);


// 注册到全局变量 $message
app.config.globalProperties.$message = Message

app.mount('#app');

如何使用 随便一个 ``.vue`` 文件中

//setup函数中使用
import { getCurrentInstance } from 'vue'

setup(){
    const { proxy } = getCurrentInstance();
    proxy.$message({ text: '登录失败', type: 'error' })
}

// proxy是全局对象,如果不是在setup中,直接使用this即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值