From 52aa9e8559865df3fc9f8b84bd7c4ffa7131088a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Tue, 4 Feb 2020 14:23:07 +0800 Subject: [PATCH] fix(Popup): may throw error when using get-container and destroyed --- src/mixins/popup/index.js | 16 +++++++++++----- src/toast/index.js | 7 ++----- src/utils/dom/node.ts | 7 +++++++ 3 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 src/utils/dom/node.ts diff --git a/src/mixins/popup/index.js b/src/mixins/popup/index.js index aa0a7369c..cb9529cf0 100644 --- a/src/mixins/popup/index.js +++ b/src/mixins/popup/index.js @@ -1,10 +1,16 @@ +// Context import { context } from './context'; +import { openOverlay, closeOverlay, updateOverlay } from './overlay'; + +// Utils +import { on, off, preventDefault } from '../../utils/dom/event'; +import { removeNode } from '../../utils/dom/node'; +import { getScroller } from '../../utils/dom/scroll'; + +// Mixins import { TouchMixin } from '../touch'; import { PortalMixin } from '../portal'; import { CloseOnPopstateMixin } from '../close-on-popstate'; -import { on, off, preventDefault } from '../../utils/dom/event'; -import { openOverlay, closeOverlay, updateOverlay } from './overlay'; -import { getScroller } from '../../utils/dom/scroll'; export const popupMixinProps = { // whether to show popup @@ -90,8 +96,8 @@ export function PopupMixin(options = {}) { beforeDestroy() { this.close(); - if (this.getContainer && this.$parent && this.$parent.$el) { - this.$parent.$el.appendChild(this.$el); + if (this.getContainer) { + removeNode(this.$el); } }, diff --git a/src/toast/index.js b/src/toast/index.js index 87ff23f38..25d5bb0a0 100644 --- a/src/toast/index.js +++ b/src/toast/index.js @@ -1,6 +1,7 @@ import Vue from 'vue'; import VueToast from './Toast'; import { isObject, isServer } from '../utils'; +import { removeNode } from '../utils/dom/node'; const defaultOptions = { icon: '', @@ -100,11 +101,7 @@ function Toast(options = {}) { clearTimeout(toast.timer); queue = queue.filter(item => item !== toast); - const parent = toast.$el.parentNode; - if (parent) { - parent.removeChild(toast.$el); - } - + removeNode(toast.$el); toast.$destroy(); }); } diff --git a/src/utils/dom/node.ts b/src/utils/dom/node.ts new file mode 100644 index 000000000..07756867a --- /dev/null +++ b/src/utils/dom/node.ts @@ -0,0 +1,7 @@ +export function removeNode(el: Node) { + const parent = el.parentNode; + + if (parent) { + parent.removeChild(el); + } +}