fix(Popup): may throw error when using get-container and destroyed

This commit is contained in:
陈嘉涵 2020-02-04 14:23:07 +08:00
parent 14ddf449a5
commit 52aa9e8559
3 changed files with 20 additions and 10 deletions

View File

@ -1,10 +1,16 @@
// Context
import { context } from './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 { TouchMixin } from '../touch';
import { PortalMixin } from '../portal'; import { PortalMixin } from '../portal';
import { CloseOnPopstateMixin } from '../close-on-popstate'; 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 = { export const popupMixinProps = {
// whether to show popup // whether to show popup
@ -90,8 +96,8 @@ export function PopupMixin(options = {}) {
beforeDestroy() { beforeDestroy() {
this.close(); this.close();
if (this.getContainer && this.$parent && this.$parent.$el) { if (this.getContainer) {
this.$parent.$el.appendChild(this.$el); removeNode(this.$el);
} }
}, },

View File

@ -1,6 +1,7 @@
import Vue from 'vue'; import Vue from 'vue';
import VueToast from './Toast'; import VueToast from './Toast';
import { isObject, isServer } from '../utils'; import { isObject, isServer } from '../utils';
import { removeNode } from '../utils/dom/node';
const defaultOptions = { const defaultOptions = {
icon: '', icon: '',
@ -100,11 +101,7 @@ function Toast(options = {}) {
clearTimeout(toast.timer); clearTimeout(toast.timer);
queue = queue.filter(item => item !== toast); queue = queue.filter(item => item !== toast);
const parent = toast.$el.parentNode; removeNode(toast.$el);
if (parent) {
parent.removeChild(toast.$el);
}
toast.$destroy(); toast.$destroy();
}); });
} }

7
src/utils/dom/node.ts Normal file
View File

@ -0,0 +1,7 @@
export function removeNode(el: Node) {
const parent = el.parentNode;
if (parent) {
parent.removeChild(el);
}
}