diff --git a/docs/examples-docs/toast.md b/docs/examples-docs/toast.md index 731b57245..9cc8d7b65 100644 --- a/docs/examples-docs/toast.md +++ b/docs/examples-docs/toast.md @@ -38,9 +38,9 @@ export default { type: 'success', message: leftSec.toString() }); - window.setInterval(() => { + const id = window.setInterval(() => { if (leftSec <= 1) { - window.clearInterval(); + window.clearInterval(id); toast.message = '跳转中...' return; } @@ -106,9 +106,9 @@ export default { type: 'success', message: leftSec.toString() }); - window.setInterval(() => { + const id = window.setInterval(() => { if (leftSec <= 1) { - window.clearInterval(); + window.clearInterval(id); toast.message = '跳转中...' return; } @@ -134,10 +134,10 @@ import { Toast } from 'src/index'; export default { methods: { showToast() { - this.toast = Toast('我是提示文案,建议不超过十五字~'); + Toast('我是提示文案,建议不超过十五字~'); }, closeToast() { - this.toast.clear(); + Toast.clear(); } } }; @@ -212,5 +212,5 @@ export default { | forbidClick | 不允许背景点击 | Boolean | false | true, false| | duration | 时长(ms) | Number | 3000ms | -| -### instanceOfToast.clear() +### Toast.clear() 关闭toast。 diff --git a/packages/toast/src/toast.js b/packages/toast/src/toast.js index 8e6674b9b..eba907550 100644 --- a/packages/toast/src/toast.js +++ b/packages/toast/src/toast.js @@ -2,24 +2,17 @@ import Vue from 'vue'; import merge from 'src/utils/merge'; const ToastConstructor = Vue.extend(require('./toast.vue')); -let toastQueue = []; +let instance; const getInstance = () => { - if (toastQueue.length > 0) { - const instance = toastQueue[0]; - toastQueue.splice(0, 1); - return instance; - } - return new ToastConstructor({ + if (instance) instance.clear(); + + instance = new ToastConstructor({ el: document.createElement('div') }); + return instance; }; -const returnInstance = instance => { - if (instance) { - toastQueue.push(instance); - } -}; const removeDom = event => { if (event.target.parentNode) { @@ -31,7 +24,6 @@ var Toast = (options = {}) => { const duration = options.duration || 3000; let instance = getInstance(); - returnInstance(instance); instance.closed = false; clearTimeout(instance.timer); instance.type = options.type ? options.type : 'text'; @@ -77,4 +69,8 @@ Toast.fail = (options) => { }, options)); }; +Toast.clear = () => { + if (instance) instance.clear(); +} + export default Toast; diff --git a/packages/toast/src/toast.vue b/packages/toast/src/toast.vue index 0e9c89035..fff1f45e0 100644 --- a/packages/toast/src/toast.vue +++ b/packages/toast/src/toast.vue @@ -1,5 +1,5 @@