Vue实现全局的toast组件

1.创建toast组件
<template>
    <div class="toast" v-if="show">
        {{ msg }}
    </div>
</template>

<style scoped>
    .toast{
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        border-radius: 3px;
        max-width: 200px;
        padding: 10px;
        background: #333;
        color: #fff;
        font-size: 14px;
        opacity: .9;
        text-align: justify;
        word-break: break-all;
        word-wrap: break-word;
    }
</style>
2.创建toast.js文件
import toast from "@/components/toast"
export default (Vue) => {
    let toastComp = Vue.extend(toast);
    function showToast(msg , duration = 3e3){
        let toastDom = new toastComp({
            data(){
                return{
                    show:true,
                    msg
                }
            }
        }).$mount()
        document.body.appendChild(toastDom.$el);
        setTimeout(() => {
            toastDom.show = false
        }, duration)
    }
    Vue.prototype.$toast = showToast;
}
3.安装并使用
import toast from "@/plugins/toast";
Vue.use(toast);


// 组件里面使用
this.$toast("message");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值