这篇文章给大家介绍使用vue插件怎么样实现一个MessageBox功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1、实现toast插件封装(类似简易版的elementUi的message)
先书写一个toast组件
<template>
<div ref="toastRef" class="toastMessageBox">{{ message }}</div>
</template>
<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator';
@Component({})
export default class toast extends Vue {
message: string = '';
type: string = '';
mounted() {
let ele: HTMLElement = <HTMLElement>this.$refs.toastRef;
if (this.type === 'success') {
ele.style.backgroundColor = '#f0f9eb';
ele.style.borderColor = '#e1f3d8';
ele.style.color = '#67c23a';
} else if (this.type === 'error') {
ele.style.backgroundColor = '#fef0f0';
ele.style.borderColor = '#fde2e2';
ele.style.color = '#f56c6c';
}
}
showToast() {
let ele: HTMLElement = <HTMLElement>this.$refs.toastRef;
ele.style.top = '20px';
ele.style.opacity = '1';
}
hideToast() {
let ele: HTMLElement = <HTMLElement>this.$refs.toastRef;
ele.style.top = '-100px';
ele.style.opacity = '0';
}
}
</script>
<style scoped lang="scss">
.toastMessageBox {
position: fixed;
min-width: 380px;
left: 50%;
z-index: 999;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
color: #fff;
padding: 15px 15px 15px 20px;
font-size: 16px;
border-radius: 4px;
opacity: 0;
top: -100px;
transition: opacity 0.3s, top 0.4s;
color: #909399;
background-color: #edf2fc;
border: 1px solid #ebeef5;
}
</style>