组件toast(类似于element-ui的message组件)的实现

实现的toast组件可以通过this.$toast()调用

需要的知识:

vue.extend();

new Vue().$mount(); //如果mount内没有要挂载的元素vue只会渲染元素而不会加载的界面上

toast.vue的内容

<!--template的内容-->
<template>
    <div>
        <slot>{{message}}</slot>
    </div>
</template>

 

//script的内容
export default {
    name: 'toast',
    data(){
        return {
            duration: 1500, //默认toast消失的时间
            message: '', //toast显示的内容
        }
    },
    mounted(){
        setTimeout(()=>{
            this.$destroy(true);
            this.$el.parentNode.removeChild(this.$el);
        }, this.duration)
    }
}

toast.js的内容

import Vue from 'vue';
import toast from './toast.vue';

let ToastConstructor = Vue.extend(Toast);

let instance;
let instances = [];

const Toast = function(options) {
    if (Vue.prototype.$isServer) return;
    options = options || {};
    if (typeof options === 'string') {
        options = {
            message: options
        };
    }
    instance = new ToastConstructor({
        data: options
    });
    instance.id = id;
    instance.$slots.default = [instance.message];
    instance.message = null;
    instance.vm = instance.$mount();
    document.body.appendChild(instance.vm.$el);
    
    instance.vm.visible = true;
    instance.dom = instance.vm.$el;
    instances.push(instance);
    return instance.vm;
};

export default Toast;

 

转载于:https://www.cnblogs.com/guojikun/p/10767614.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值