代码可以直接粘贴
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即可