vue3项目封装Message组件

效果

类似于elementUI中的$messageBox,给用户进行界面提示

 功能分析

固定顶部显示,有三种类型:成功,错误,警告。

显示消息提示时需要动画从上滑入。

组件使用的方式不够便利,封装成工具函数方式。


步骤

1.定义组件

<template>
  <div class="xtx-message" :style="style[type]">
    <!-- 上面绑定的是样式 -->
    <!-- 不同提示图标会变 -->
    <i class="iconfont" :class="[style[type].icon]"></i>
    <span class="text">{{text}}</span>
  </div>
</template>
<script>
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)'
      }
    }
    return { style }
  }
}
</script>
<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>

2.控制显示隐藏

定义一个数据控制显示隐藏,默认是隐藏,组件挂载完毕显示

<template>
   <div class='xtx-message' :style="style"
+       v-show="visible">
   </div>
</template>

<script>
 import { ref, onMounted } from 'vue'
export default {
    name: 'XtxMessage',
    const visible = ref(false)
    onMounted(()=>{
        visible.value = true
    })
    return { visible }
  }
}
</script>

3.动画效果

vue框架中,使用transition组件,实现动画效果

<template>
<!-- 目标:它从无到有是有动画过渡效果
    <transition name="down">: vue会元素可见和不可见的关键节点 自动补充 特殊的css类名
  -->
+  <Transition name="down">
    <div class='xtx-message' :style="style" v-show="visible">
      <!-- 上面绑定的是样式 -->
      <!-- 不同提示图标会变 -->
      <i class="iconfont" :class="[style[type].icon]"></i>
      <span class="text">{{text}}</span>
    </div>
+  </Transition>
</template>

css样式

<style scoped lang='less'>
.down-enter-from {
  transform: translate3d(0,-75px,0);
  opacity: 0;
}
.down-enter-active {transition: all 0.5s;}
.down-enter-to { transform: none; opacity: 1;}

// transform: none; opacity: 1;
// 省略其他....
</style>

4.封装成函数调用

基本步骤:

1. 导入组件
----- 显示 ------
2. 根据组件创建虚拟节点. const vnode = createVNode(XtxMessage, { type, text })
3. 准备一个DOM容器
4. 把虚拟节点渲染DOM容器中. render(vnode, div)
-----隐藏DOM-----
5. 开启定时器,移出DOM容器内容 render(null, div)

4.1 创建Message.js

// 导入.vue组件,导出一个函数,方便我们直接以函数的方式来调用组件

// 1. 导入组件
// ----- 显示 ------
// 2. 根据组件创建虚拟节点. const vnode = createVNode(XtxMessage, { type, text })
// 3. 准备一个DOM容器
// 4. 把虚拟节点渲染DOM容器中. render(vnode, div)
// -----隐藏DOM-----
// 5. 开启定时器,移出DOM容器内容 render(null, div)
import { createVNode, render } from 'vue'
import XtxMessage from './xtx-message.vue'

// 2. 准备一个DOM容器
const div = document.createElement('div')
div.setAttribute('class', 'xtx-message-wrapper')
document.body.appendChild(div)

let time = null
export default ({ text, type }) => {
  // 3. 创建虚拟dom          (组件对象, props)
  const vnode = createVNode(XtxMessage, { text, type })
  // 4. 把虚拟dom渲染到div
  render(vnode, div)

  // 5. 设置定时器清空
  clearTimeout(time)
  time = setTimeout(() => {
    render(null, div)
  }, 2000)

  console.log('message.js')
}

4.2 使用

import Message from '@/components/Message'

Message({ type: 'error', text: '登录失败' })

5.vue3全局挂载

类似于Vue2中的插件做法(Vue.prototype.xxxx = function(){} )

由于vue3中组合式api内没有this,所以使用全局挂载的方法其实也不方便,所以,在用Message还是先import,再使用。

5.1 main.js

import Message from '@/components/Message'
app.config.globalProperties.$message = Message

5.2 使用

选项式api(vue2)

created() {
	this.$message({ type: 'error', text: '登录失败' })
}

组合式api

import { getCurrentInstance } from 'vue'
export default {

  setup () {
    const internalInstance = getCurrentInstance()

    internalInstance.appContext.config.globalProperties.$message({ type: 'success', text: 'xxx' })
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值