目的:用于主动操作后的反馈提示
element-ui 组件参考
组件功能分析:
- 固定顶部显示,有三种类型:成功,错误,警告。
- 显示消息提示时需要动画从上滑入且淡出。
- 组件使用的方式不够便利,封装成工具函数方式。
1.封装组件
src/components/mesage.vue
<template>
<!-- vue动画:从上滑入且淡出 -->
<Transition name="down">
<!-- style 绑定的是样式 -->
<div class="message" :style="style[type]" v-show="visible">
<!-- 不同提示图标会变 -->
<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: 'Message',
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)'
}
}
// 定义一个数据控制显示隐藏,默认是隐藏,组件挂载完毕显示
const visible = ref(false)
onMounted(() => { // 需调用钩子函数,等dom渲染完成后,再进行赋值,如果在setup中直接赋值,则会被直接渲染成true
visible.value = true
})
return { style, visible }
}
}
</script>
<style scoped lang="less">
.down {
&-enter {
&-from {
transform: translate3d(0,-75px,0);
opacity: 0;
}
&-active {
transition: all 0.5s;
}
&-to {
transform: none;
opacity: 1;
}
}
}
.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.封装成vue实例函数式调用
src/components/Message.js
// 实现使用函数调用xtx-message组件的逻辑
import { createVNode, render } from 'vue'
import Message from './message.vue'
// 准备dom容器
const div = document.createElement('div')
div.setAttribute('class', 'message-container')
document.body.appendChild(div)
// 定时器标识
let timer = null
export default ({ type, text }) => {
// 实现:根据xtx-message.vue渲染消息提示
// 1. 导入组件
// 2. 根据组件创建虚拟节点
const vnode = createVNode(Message, { type, text })
// 3. 准备一个DOM容器
// 4. 把虚拟节点渲染挂载到DOM容器中
render(vnode, div)
// 5. 开启定时,移出DOM容器内容
clearTimeout(timer)
timer = setTimeout(() => {
render(null, div) // 3秒后将组件从div上移出
}, 3000)
}
全局注册
src/components/index.js
import Message from './Message'
export default {
install (app) {
// vue3.0如果你想挂载全局的属性,能够通过组件实例调用的属性 this.$message(element-ui)
app.config.globalProperties.$message = Message// 原型函数
}
}
src/main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 引入全局注册组件
import MyUI from '@/components'
createApp(App).use(store).use(router).use(MyUI).mount('#app')
3.在逻辑页面使用
<template>
<!-- 测试组件 -->
<button @click="doLogin">登录</button>
</template>
<script>
import Message from '@/components/Message'
export default {
name: 'Test',
setup () {
const doLogin = () => { // 点击登录按钮,触发回调函数
Message({ type: 'error', text: '登录失败' })
}
return { doLogin }
},
mounted () {
// 如果想试用this.$message,须在mounted钩子函数中,setup中没有this实例,但vue3.0中还是建议在setup函数中进行逻辑操作
this.$message({ type: 'error', text: '登录失败' })
}
}
</script>
使用优化
<template>
<!-- 测试组件 -->
<button @click="doLogin">登录</button>
</template>
<script>
import { getCurrentInstance } from 'vue'
export default {
name: 'Test',
setup () {
const instance = getCurrentInstance() // vue3提供的方法,创建类似于this的实例
const doLogin = () => {
instance.proxy.$message({ type: 'error', text: '登录失败' }) // 类似于this.$message()
}
return { doLogin }
},
mounted () {
this.$message({ type: 'error', text: '登录失败' })
}
}
</script>