Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vant Toast tips


May 07, 2021 Vant


Table of contents


Introduced

import Vue from 'vue';
import { Toast } from 'vant';

Vue.use(Toast);

Code demo

Text tips

Toast('提示内容');

Load prompt

Using the Toast.loading method to demonstrate loading tips, background clicks can be disabled with forbidClick properties, and load icon types can be customized with the loading Type property.

Toast.loading({
  message: '加载中...',
  forbidClick: true
});

// 自定义加载图标
Toast.loading({
  message: '加载中...',
  forbidClick: true,
  loadingType: 'spinner'
});

Success/failure prompt

Toast.success('成功文案');
Toast.fail('失败文案');

Custom icons

Toast({
  message: '自定义图标',
  icon: 'like-o'
});

Toast({
  message: '展示图片',
  icon: 'https://img.yzcdn.cn/vant/logo.png'
});

Dynamic update tips

const toast = Toast.loading({
  duration: 0, // 持续展示 toast
  forbidClick: true,
  message: '倒计时 3 秒'
});

let second = 3;
const timer = setInterval(() => {
  second--;
  if (second) {
    toast.message = `倒计时 ${second} 秒`;
  } else {
    clearInterval(timer);
    // 手动清除 Toast
    Toast.clear();
  }
}, 1000);

In-component calls

When the toast component is introduced, the method is automatically mounted on Vue'$toast prototype for easy in-component calls.

export default {
  mounted() {
    this.$toast('提示文案');
  }
}

Singleton mode

Toast defaults to single-case mode, where only one toast exists at a time, and if you need to pop up multiple toasts at the same time, you can refer to the following example

Toast.allowMultiple();

const toast1 = Toast('第一个 Toast');
const toast2 = Toast.success('第二个 Toast');

toast1.clear();
toast2.clear();

Modify the default configuration

The Toast's default configuration can be modified globally through the Toast.setDefaultOptions function

// 将所有 Toast 的展示时长设置为 2000 毫秒
Toast.setDefaultOptions({ duration: 2000 });

// 将所有 loading Toast 设置为背景不可点击 (2.2.10 版本开始支持)
Toast.setDefaultOptions('loading', { forbidClick: true });

// 重置所有 Toast 的默认配置
Toast.resetDefaultOptions();

// 重置 loading Toast 的默认配置 (2.2.10 版本开始支持)
Toast.resetDefaultOptions('loading');

Api

Method

The method name Description Parameters Returns a value
Toast Show tips options | message Toast instance
Toast.loading Show loading tips options | message Toast instance
Toast.success Show success tips options | message Toast instance
Toast.fail Show failure prompts options | message Toast instance
Toast.clear Turn off the prompt clearAll: boolean void
Toast.allowMultiple Multiple toasts are allowed to exist at the same time - void
Toast.setDefaultOptions Modify the default configuration to take effect for all toasts.
The incoming type modifies the default configuration of the specified type
type | options void
Toast.resetDefaultOptions Reset the default configuration to take effect for all toasts.
The incoming type resets the default configuration of the specified type
type void

Options

Parameters Description Type The default
type Tip type, optional value loading success
fail html
string text
position Location, the optional value top bottom string middle
message Text content, supported \n string ''
icon v2.0.1 Custom icons that support incoming icon names or picture links string -
iconPrefix v2.0.9 Icon class name prefix string van-icon
overlay v2.2.13 Whether to display the background matte layer boolean false
forbidClick Whether background clicking is prohibited boolean false
closeOnClick v2.1.5 Whether to close after clicking boolean false
closeOnClickOverlay v2.2.13 Whether to close after tapping the matte layer boolean false
loadingType Load icon type, optional value spinner string circular
duration The presentation length (ms), when the value is 0, the toast does not disappear number 2000
className Custom class name any -
onOpened A fully displayed callback function Function -
onClose The callback function when closed Function -
transition v2.2.6 The name of the animation class, equivalent to the name property of name string van-fade
getContainer Specify the mounted node, using an example string | () => Element body


Example demonstration