组件功能分析:
固定顶部显示,有三种类型:成功,错误,警告。
显示消息提示时需要动画从上滑入且淡出。
组件使用的方式不够便利,封装成工具函数方式。
1.定义组件:src/components/library/b2c-message.vue
<template>
<Transition name="down">
<div class="xtx-message" :style="style[type]">
<!-- 上面绑定的是样式 -->
<!-- 不同提示图标会变 -->
<i class="iconfont" :class="[style[type].icon]"></i>
<span class="text">{
{text}}</span>
</div>
</Transition>
</template>
<script>
import { onMounted, ref } from 'vue'
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;
}
}
//动画
.down {
&-enter {
&-from {
transform: translate3d(0,-75px,0);
opacity: 0;
}
&-active {
transition: all 0.5s;
}
&-to {
transform: none;
opacity: 1;
}
}
}
</style>
2.封装成vue实例函数式调用
vue3.0使用app.config.globalProperties挂载原型方法
也支持直接导入函数使用
src/components/library/Message.js
// 提供一个能够显示b2c-message组件的函数
// 这个函数将来:导入直接使用,也可以挂载在vue实例原型上
// import Message from 'Message.js ' 使用 Message({type : 'error ' , text: '提示文字})
// 如果挂载到原型上,使用:this.$message({type : 'error ' , text: '提示文字})
// vue中有一个函数createVNode可以把组件编译成dom节点,createVNode('组件','给组件传递得参数')
import { createVNode, render } from 'vue'
import B2cMessage from './b2c-message'
// 准备装载dom得容器
const div = document.createElement('div')// 创建div
div.setAttribute('class', 'b2c-message-container')// 起名字
document.body.appendChild(div)// 把div加到body中
// 定时器标识
let timer = null
export default ({ type, text }) => {
// 渲染组件
// 1.导入消息提示组件
// 2.将消息提示组件编译为虚拟节点(dom节点),因为组件是.vue,需要编译成html,css,js变成dom
const vnode = createVNode(B2cMessage, { type, text })
// 3.准备一个装载消息提示组件的dom容器来装dom节点
// 4.将虚拟节点渲染在容器中,render(vue的虚拟节点,dom容器)
render(vnode, div)
// 5. 开启定时,移出DOM容器内容
clearTimeout(timer)
timer = setTimeout(() => {
render(null, div)
}, 3000)
}
src/components/library/index.js
// 扩展vue原有的功能:全局组件,自定义指令,挂载原型方法
// vue2.0插件写法要素:导出一个对象,有install函数,默认传入了Vue构造函数,Vue基础之上扩展
// vue3.0插件写法要素:导出一个对象,有install函数,默认传入了app应用实例,app基础之上扩展
import Message from './Message' // 引入消息提示信息
export default {
install (app) {
// 如果你想挂载全局的属性,能够通过组件实例调用的属性 this.$message
app.config.globalProperties.$message = Message// 原型函数
}
}
main.js
import { createApp } from 'vue'
import App from './App.vue'
import UI from './components/library'// 引入全局用的插件
// 插件的使用,在main.js使用app.use(插件)
createApp(App).use(UI).mount('#app')
<script>
// 引入消息提示函数
import Message from '@/components/library/Message'
//import { getCurrentInstance } from 'vue'
export default {
setup () {
// 1.vue3.0组合式api,setup使用 引入Message({})
Message({ type: 'error', text: 'error' })
//3. 不用选项式api怎么调原型上的方法?setup中怎么调用原型上的方法?
// const { proxy } = getCurrentInstance() // 获取当前组件实例proxy,proxy是从应用实例app上结构出来的
// proxy.$message({ type: 'error', text: 'error!!!!!!!!!!!!!!!!!!!!!' })
return {}
}
// 2.vue2.0 选项式api用 原型上的this.$message
// created () {
// this.$message({ type: 'error', text: 'error!!!!!' })
// }
}
</script>