fix(Toast): ssr error (#8214)

This commit is contained in:
neverland 2021-02-25 10:13:36 +08:00 committed by GitHub
parent f746775a0d
commit c054164ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
import { ref, App, TeleportProps, getCurrentInstance } from 'vue'; import { ref, App, TeleportProps, getCurrentInstance } from 'vue';
import { isObject, inBrowser } from '../utils'; import { isObject, inBrowser, ComponentInstance } from '../utils';
import { mountComponent, usePopupState } from '../utils/mount-component'; import { mountComponent, usePopupState } from '../utils/mount-component';
import VanToast, { ToastType, ToastPosition } from './Toast'; import VanToast, { ToastType, ToastPosition } from './Toast';
import type { LoadingType } from '../loading'; import type { LoadingType } from '../loading';
@ -47,8 +47,7 @@ const defaultOptions: ToastOptions = {
closeOnClickOverlay: false, closeOnClickOverlay: false,
}; };
// TODO remove any let queue: ComponentInstance[] = [];
let queue: any[] = [];
let allowMultiple = false; let allowMultiple = false;
let currentOptions = { ...defaultOptions }; let currentOptions = { ...defaultOptions };
@ -104,11 +103,6 @@ function createInstance() {
} }
function getInstance() { function getInstance() {
/* istanbul ignore if */
if (!inBrowser) {
return {};
}
if (!queue.length || allowMultiple) { if (!queue.length || allowMultiple) {
const instance = createInstance(); const instance = createInstance();
queue.push(instance); queue.push(instance);
@ -118,6 +112,10 @@ function getInstance() {
} }
function Toast(options: string | ToastOptions = {}) { function Toast(options: string | ToastOptions = {}) {
if (!inBrowser) {
return {} as ComponentInstance;
}
const toast = getInstance(); const toast = getInstance();
const parsedOptions = parseOptions(options); const parsedOptions = parseOptions(options);
@ -150,7 +148,7 @@ Toast.clear = (all?: boolean) => {
} else if (!allowMultiple) { } else if (!allowMultiple) {
queue[0].clear(); queue[0].clear();
} else { } else {
queue.shift().clear(); queue.shift()!.clear();
} }
} }
}; };