/*! For license information please see 4127.ab7a37a1.js.LICENSE.txt */ (self.webpackChunk=self.webpackChunk||[]).push([["4127"],{54467:function(t,s,n){"use strict";n.r(s);var a=n("80681");let e=["innerHTML"];s.default={setup:()=>({html:""}),render:()=>((0,a.wg)(),(0,a.iD)("div",{class:"van-doc-markdown-body",innerHTML:'

Toast

\n

Intro

\n

Black semi-transparent pop-up hint in the middle of the page, used for message notification, loading hint, operation result hint and other scenarios.

\n

Install

\n

Register component globally via app.use, refer to Component Registration for more registration ways.

\n
import { createApp } from 'vue';\nimport { Toast } from 'vant';\n\nconst app = createApp();\napp.use(Toast);\n
\n

Function Call

\n

Vant provides some utility functions that can quickly evoke global Toast components.

\n

For example, calling the showToast function will render a Toast directly in the page.

\n
import { showToast } from 'vant';\n\nshowToast('Some messages');\n
\n

Usage

\n

Text

\n

Use the showToast method to display a text toast in the center of the screen.

\n
import { showToast } from 'vant';\n\nshowToast('Some messages');\n
\n

Loading

\n

Use the showLoadingToast method to display a loading toast. The forbidClick option can be used to disable background clicks.

\n
import { showLoadingToast } from 'vant';\n\nshowLoadingToast({\n  message: 'Loading...',\n  forbidClick: true,\n});\n
\n

Success/Fail

\n

Use the showSuccessToast method to display a success message, and use the showFailToast method to display a failure message.

\n
import { showSuccessToast, showFailToast } from 'vant';\n\nshowSuccessToast('Success');\nshowFailToast('Fail');\n
\n

Custom Icon

\n

The icon option allows you to customize the icon by specifying either the icon name or an image URL, which is equivalent to the name attribute of the Icon component.

\n
import { showToast, showLoadingToast } from 'vant';\n\nshowToast({\n  message: 'Custom Icon',\n  icon: 'like-o',\n});\n\nshowToast({\n  message: 'Custom Image',\n  icon: 'https://fastly.jsdelivr.net/npm/@vant/assets/logo.png',\n});\n
\n

The loadingType option allows you to customize the type of loading icon.

\n
showLoadingToast({\n  message: 'Loading...',\n  forbidClick: true,\n  loadingType: 'spinner',\n});\n
\n

Custom Position

\n

By default, the Toast is rendered in the center of the screen. You can control the position of the Toast by using the position option.

\n
import { showToast } from 'vant';\n\nshowToast({\n  message: 'Top',\n  position: 'top',\n});\n\nshowToast({\n  message: 'Bottom',\n  position: 'bottom',\n});\n
\n

Word Break

\n

The wordBreak option controls how the text in the Toast is truncated when it exceeds the available space. The default value is break-all, and the optional values are break-word and normal.

\n
import { showToast } from 'vant';\n\nshowToast({\n  message: 'This message will contain a incomprehensibilities long word.',\n  wordBreak: 'break-all',\n});\n\nshowToast({\n  message: 'This message will contain a incomprehensibilities long word.',\n  wordBreak: 'break-word',\n});\n
\n

Update Message

\n

When executing the Toast method, it returns the corresponding Toast instance. You can dynamically update the message by modifying the message property on the instance.

\n
import { showLoadingToast, closeToast } from 'vant';\n\nconst toast = showLoadingToast({\n  duration: 0,\n  forbidClick: true,\n  loadingType: 'spinner',\n  message: '3 seconds',\n});\n\nlet second = 3;\nconst timer = setInterval(() => {\n  second--;\n  if (second) {\n    toast.message = `${second} seconds`;\n  } else {\n    clearInterval(timer);\n    closeToast();\n  }\n}, 1000);\n
\n

Singleton

\n

The Toast is implemented as a singleton by default, which means that only one Toast can exist at a time. If you need to display multiple Toasts at the same time, you can refer to the following example:

\n
import { showToast, showSuccessToast, allowMultipleToast } from 'vant';\n\nallowMultipleToast();\n\nconst toast1 = showToast('First Toast');\nconst toast2 = showSuccessToast('Second Toast');\n\ntoast1.close();\ntoast2.close();\n
\n

Set Default Options

\n

You can globally modify the default configuration of the showToast and other methods by using the setToastDefaultOptions function.

\n
import { setToastDefaultOptions, resetToastDefaultOptions } from 'vant';\n\nsetToastDefaultOptions({ duration: 2000 });\n\nsetToastDefaultOptions('loading', { forbidClick: true });\n\nresetToastDefaultOptions();\n\nresetToastDefaultOptions('loading');\n
\n

Use Toast Component

\n

If you need to embed components or other custom content within the Toast, you can directly use the Toast component and customize it using the message slot. Before using it, you need to register the component using app.use or other methods.

\n
<van-toast v-model:show="show" style="padding: 0">\n  <template #message>\n    <van-image :src="image" width="200" height="140" style="display: block" />\n  </template>\n</van-toast>\n
\n
import { ref } from 'vue';\n\nexport default {\n  setup() {\n    const show = ref(false);\n    return { show };\n  },\n};\n
\n

API

\n

Methods

\n

Vant exports following Toast utility functions:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescriptionAttributeReturn value
showToastDisplay a text toastToastOptions | stringToast instance
showLoadingToastDisplay a loading toastToastOptions | stringToast instance
showSuccessToastDisplay a success toastToastOptions | stringToast instance
showFailToastDisplay a fail toastToastOptions | stringToast instance
closeToastClose the currently displayed toastcloseAll: booleanvoid
allowMultipleToastAllow multiple toasts to be displayed as the same time-void
setToastDefaultOptionsModify the default configuration that affects all showToast calls. Specify the type parameter to modify the default configuration of a specific type of toasttype | ToastOptionsvoid
resetToastDefaultOptionsReset the default configuration that affects all showToast calls. Specify the type parameter to reset the default configuration of a specific type of toasttypevoid
\n

ToastOptions

\n

When calling the showToast and other related methods, the following options are supported:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeDescriptionTypeDefault
typeCan be set to loading success fail htmlToastTypetext
positionCan be set to top middle bottomToastPositionmiddle
messageMessagestring\'\'
wordBreakCan be set to normal break-all break-wordToastWordBreak\'break-all\'
iconCustom iconstring-
iconSizeCustom icon sizenumber | string36px
iconPrefixIcon className prefixstringvan-icon
overlayWhether to show overlaybooleanfalse
forbidClickWhether to forbid click backgroundbooleanfalse
closeOnClickWhether to close after clickedbooleanfalse
closeOnClickOverlayWhether to close when overlay is clickedbooleanfalse
loadingTypeLoading icon type, can be set to spinnerstringcircular
durationToast duration(ms), won\'t disappear if value is 0number2000
classNameCustom classNamestring | Array | object-
overlayClassCustom overlay classstring | Array | object-
overlayStyleCustom overlay styleobject-
transitionTransition, equivalent to name prop of transitionstringvan-fade
teleportSpecifies a target element where Toast will be mountedstring | Elementbody
zIndexSet the z-index to a fixed valuenumber | string2000+
onCloseCallback function after closeFunction-
onOpenedCallback function after openedFunction-
\n

Props

\n

When using Toast as a component, the following props are supported:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeDescriptionTypeDefault
v-model:showWhether to show toastbooleanfalse
typeCan be set to loading success fail htmlToastTypetext
positionCan be set to top middle bottomToastPositionmiddle
messageMessagestring\'\'
word-breakCan be set to normal break-all break-wordToastWordBreak\'break-all\'
iconCustom iconstring-
icon-sizeCustom icon sizenumber | string36px
icon-prefixIcon className prefixstringvan-icon
overlayWhether to show overlaybooleanfalse
forbid-clickWhether to forbid click backgroundbooleanfalse
close-on-clickWhether to close after clickedbooleanfalse
close-on-click-overlayWhether to close when overlay is clickedbooleanfalse
loading-typeLoading icon type, can be set to spinnerstringcircular
durationToast duration(ms), won\'t disappear if value is 0number2000
class-nameCustom classNamestring | Array | object-
overlay-classCustom overlay classstring | Array | object-
overlay-styleCustom overlay styleobject-
transitionTransition, equivalent to name prop of transitionstringvan-fade
teleportSpecifies a target element where Toast will be mountedstring | Elementbody
z-indexSet the z-index to a fixed valuenumber | string2000+
\n

Events

\n

When using Toast as a component, the following events are supported:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
EventDescriptionParameters
closeCallback function after close-
openedCallback function after opened-
\n

Slots

\n

You can use following slots when using Toast component:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescription
messageCustom message
\n

Types

\n

The component exports the following type definitions:

\n
import type {\n  ToastType,\n  ToastProps,\n  ToastOptions,\n  ToastPosition,\n  ToastWordBreak,\n  ToastWrapperInstance,\n} from 'vant';\n
\n

Theming

\n

CSS Variables

\n

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDefault ValueDescription
--van-toast-max-width70%-
--van-toast-font-sizevar(--van-font-size-md)-
--van-toast-text-colorvar(--van-white)-
--van-toast-loading-icon-colorvar(--van-white)-
--van-toast-line-heightvar(--van-line-height-md)-
--van-toast-radiusvar(--van-radius-lg)-
--van-toast-backgroundfade(var(--van-black), 70%)-
--van-toast-icon-size36px-
--van-toast-text-min-width96px-
--van-toast-text-paddingvar(--van-padding-xs) var(--van-padding-sm)-
--van-toast-default-paddingvar(--van-padding-md)-
--van-toast-default-width88px-
--van-toast-default-min-height88px-
--van-toast-position-top-distance20%-
--van-toast-position-bottom-distance20%-
\n
'},null,8,e))}}}]);