diff --git a/packages/toast/toast.ts b/packages/toast/toast.ts index 39207f8e..1488726b 100644 --- a/packages/toast/toast.ts +++ b/packages/toast/toast.ts @@ -1,15 +1,14 @@ import { isObj } from '../common/validator'; type ToastMessage = string | number; +type ToastContext = WechatMiniprogram.Component.TrivialInstance | WechatMiniprogram.Page.TrivialInstance interface ToastOptions { show?: boolean; type?: string; mask?: boolean; zIndex?: number; - context?: - | WechatMiniprogram.Component.TrivialInstance - | WechatMiniprogram.Page.TrivialInstance; + context?: () => ToastContext | ToastContext; position?: string; duration?: number; selector?: string; @@ -50,7 +49,7 @@ function Toast(toastOptions: ToastOptions | ToastMessage) { ...parseOptions(toastOptions), } as ToastOptions; - const context = options.context || getContext(); + const context = (typeof options.context === 'function' ? options.context() : options.context) || getContext(); const toast = context.selectComponent(options.selector as string); if (!toast) {