[bugfix] Toast: render error when use getContainer (#3115)

This commit is contained in:
neverland 2019-04-09 22:46:57 +08:00 committed by GitHub
parent 2830f4cacd
commit b7b15fa183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 9 deletions

View File

@ -1,6 +1,6 @@
import Vue from 'vue';
import VanDialog from './Dialog';
import { isServer } from '../utils';
import { isServer, isInDocument } from '../utils';
let instance;
@ -29,7 +29,7 @@ function Dialog(options) {
}
return new Promise((resolve, reject) => {
if (!instance || !document.body.contains(instance.$el)) {
if (!instance || !isInDocument(instance.$el)) {
initInstance();
}

View File

@ -1,6 +1,6 @@
import Vue from 'vue';
import VueToast from './Toast';
import { isObj, isServer } from '../utils';
import { isObj, isServer, isInDocument } from '../utils';
const defaultOptions = {
type: 'text',
@ -19,7 +19,7 @@ const defaultOptions = {
const parseOptions = message => (isObj(message) ? message : { message });
let queue = [];
let singleton = true;
let multiple = false;
let currentOptions = { ...defaultOptions };
function createInstance() {
@ -28,12 +28,13 @@ function createInstance() {
return {};
}
if (!queue.length || !singleton) {
if (!queue.length || multiple || !isInDocument(queue[0].$el)) {
const toast = new (Vue.extend(VueToast))({
el: document.createElement('div')
});
queue.push(toast);
}
return queue[queue.length - 1];
}
@ -56,7 +57,7 @@ function Toast(options = {}) {
options.onClose();
}
if (!singleton && !isServer) {
if (multiple && !isServer) {
clearTimeout(toast.timer);
queue = queue.filter(item => item !== toast);
@ -97,7 +98,7 @@ Toast.clear = all => {
toast.clear();
});
queue = [];
} else if (singleton) {
} else if (!multiple) {
queue[0].clear();
} else {
queue.shift().clear();
@ -113,8 +114,8 @@ Toast.resetDefaultOptions = () => {
currentOptions = { ...defaultOptions };
};
Toast.allowMultiple = (allow = true) => {
singleton = !allow;
Toast.allowMultiple = (value = true) => {
multiple = value;
};
Toast.install = () => {

View File

@ -44,3 +44,7 @@ export function isIOS(): boolean {
export function range(num: number, min: number, max: number): number {
return Math.min(Math.max(num, min), max);
}
export function isInDocument(element: HTMLElement): boolean {
return document.body.contains(element);
}