1. 创建Toast组件
<template>
<div class="toast">{{message}}</div>
</template>
<script>
export default {
props:['message']
}
</script>
<style lang="less" scoped>
.toast{
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
padding:10px;
background: rgba(0, 0, 0, .35);
border-radius: 5px;
color: #fff;
}
</style>>
2.引用
<Toast v-if="isShowToast" :message = "toastMessage"/>
import Toast from '../../components/toast'
data() {
return {
isShowToast:false,
toastMessage: ''
};
},
method:{
showToast(data){
this.isShowToast = true;
this.toastMessage=data
setTimeout(()=>{
this.isShowToast = false;
this.toastMessage = ""
},2000)
},
}
3.使用
this.showToast("登录成功")