import ToastVue from './src/toast.vue' let instance: any let Toast = { install(Vue) { Vue.prototype.$toast = function (opts = {}) { let defaultOpts = { text: '', type: '', cover: false, duration: 2500, callback: null as any, clean: false } for(var key in opts){ if(opts.hasOwnProperty(key) === true){ defaultOpts[key]=opts[key]; } } var options = JSON.parse(JSON.stringify(defaultOpts)) // var options = Object.assign(defaultOpts, opts); if (!instance) { instance = new ToastVue({ el: document.createElement('div') }) } // 关闭所有的 toast if (options.clean) { instance.show = false return } if (instance.show && !options.cover) return instance.text = options.text instance.type = options.type document.body.appendChild(instance.$el) instance.show = true clearTimeout(this.time) // 永不关闭 if (options.duration === 'forever') { return } this.time = setTimeout(() => { instance.show = false if (options.callback) { options.callback(options) } }, options.duration) } } } export default Toast