参考 Element Plus 组件库:Message 消息提示组件
组件功能分析:
- 固定顶部显示,有三种类型:成功,错误,警告。
- 组件使用的方式不够便利,封装成工具函数方式。
- 显示消息提示时需要动画从上滑入且淡出。
定义组件:
src/components/library/xtx-message.vue
<template>
<transition name="down">
<div class="xtx-message" :style="style[type]" v-if="isShow">
<!-- 上面绑定的是样式 -->
<!-- 不同提示图标会变 -->
<i class="iconfont" :class="style[type].icon"></i>
<span class="text">{{message}}</span>
</div>
</transition>
</template>
<script>
import { onMounted, ref } from 'vue-demi'
export default {
name: 'XtxMessage',
props: {
message: {
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: translateY(-75px);
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: 75px;
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>
使用组件:
<XtxMessage message="手机号或密码错误" type="error" />
封装成vue实例函数式调用
src/components/library/Message.js
// 需求:XtXMessage({ message: '恭喜你,这是一条成功消息', type: 'success' })
import { createVNode, render } from 'vue'
import XtxMessage from './xtx-message.vue'
// 方案一: 准备DOM容器 - 传统方式
// const div = document.createElement('div')
// div.setAttribute('class', 'xtx-message-container')
// document.body.appendChild(div)
// 方案二:准备DOM容器 createVNode(h) + render 动态创建标签或者组件
const divVNode = createVNode('div', { class: 'xtx-message-container' })
render(divVNode, document.body)
// ? 虚拟DOM(对象)成功写入到页面后,可通过 el 属性获取到真实DOM节点
const div = divVNode.el
// console.log('虚拟DOM', divVNode)
// console.log('真实DOM', div)
const XtXMessage = ({ message, type }) => {
// 1. 动态创建虚拟DOM - createVNode(h) 函数
const comVNode = createVNode(XtxMessage, { message, type })
// 2. 渲染到body页面中 - render 函数
// render(comVNode, document.body)
render(comVNode, div)
// 3.提示在 3s 后自动卸载
setTimeout(() => {
render(null, div)
}, 3000)
}
export default XtXMessage
vue3.0 使用 app.config.globalProperties 挂载原型方法
src/components/library/index.js
import Message from './Message.js'
// Vue3 注册全局组件库写法:
// app.component(组件名,组件文件)
// 导出一个配置,用于 app.use() 安装组件库使用
export default {
install (app) {
// 全局挂载 原型函数 过组件实例调用的属性 this.$message
app.config.globalProperties.$message = Message
}
}
main.js 注册
// 导入自己封装的组件
import XtxUI from '@/components/library/index'
// 创建 Vue 应用实例
const app = createApp(App)
// 注册,安装自己的组件库
app.use(XtxUI)
使用消息组件
import Message from '@/components/library/Message.js'
export default {
setup () {
Message({ type: 'success', message: '发送成功' })
}
版权声明:本文为m0_50125215原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。