### Integrate Vant via CDN
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/quickstart.en-US.md
Example of including Vant in a standard HTML file via CDN links and initializing components globally.
```html
```
--------------------------------
### Install Auto-Import Plugins
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/quickstart.en-US.md
Commands to install necessary plugins for automatic component and style importing in build tools.
```bash
# with npm
npm i @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
# with yarn
yarn add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
# with pnpm
pnpm add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
# with Bun
bun add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
```
--------------------------------
### Integrate Vant into Nuxt 3
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/quickstart.en-US.md
Shows the installation and configuration steps for using Vant in a Nuxt 3 project via the @vant/nuxt module.
```bash
npm i @vant/nuxt -D
```
```js
export default defineNuxtConfig({
modules: ['@vant/nuxt'],
});
```
```html
button
```
--------------------------------
### Clone Repository and Install Dependencies
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/contribution.en-US.md
Steps to clone the Vant repository, set up the pnpm package manager, install dependencies, and start local development.
```bash
git clone git@github.com:vant-ui/vant.git
corepack enable
pnpm i
pnpm dev
```
--------------------------------
### Usage of Auto-Imported Vant Components
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/quickstart.en-US.md
Demonstrates how to use Vant components and APIs in Vue templates and scripts after configuring the auto-import plugins.
```html
```
--------------------------------
### PickerGroup Setup Script
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/picker-group/README.zh-CN.md
Provides the Vue 3 setup script for the PickerGroup controlled mode example. It includes refs for active tab, date, time, and handler functions for tab switching, confirmation, and cancellation.
```javascript
import { ref } from 'vue';
import { showToast } from 'vant';
export default {
setup() {
const activeTab = ref(0);
const currentDate = ref(['2022', '06', '01']);
const currentTime = ref(['12', '00']);
const setActiveTab = () => {
activeTab.value = activeTab.value ? 0 : 1;
};
const onConfirm = () => {
showToast(
`${currentDate.value.join('/')} ${currentTime.value.join(':')}`,
);
};
const onCancel = () => {
showToast('cancel');
};
return {
minDate: new Date(2020, 0, 1),
maxDate: new Date(2025, 5, 1),
activeTab,
currentDate,
currentTime,
setActiveTab,
onConfirm,
onCancel,
};
},
};
```
--------------------------------
### Toast - Introduction and Installation
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/toast/README.zh-CN.md
Introduction to the Toast component and how to install it globally in a Vue application.
```APIDOC
## Toast轻提示
### 介绍
在页面中间弹出黑色半透明提示,用于消息通知、加载提示、操作结果提示等场景。
### 引入
通过以下方式来全局注册组件,更多注册方式请参考[组件注册](#/zh-CN/advanced-usage#zu-jian-zhu-ce)。
```js
import { createApp } from 'vue';
import { Toast } from 'vant';
const app = createApp();
app.use(Toast);
```
```
--------------------------------
### Install @vant/popperjs with Bun
Source: https://github.com/youzan/vant/blob/main/packages/vant-popperjs/README.md
Use this command to install the package with Bun.
```shell
bun add @vant/popperjs
```
--------------------------------
### Install Vant Area Data via Bun
Source: https://github.com/youzan/vant/blob/main/packages/vant-area-data/README.md
Install the package using Bun.
```shell
bun add @vant/area-data
```
--------------------------------
### Install Vant with Yarn, pnpm, or Bun
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/quickstart.zh-CN.md
Alternative package managers for installing Vant.
```bash
yarn add vant
pnpm add vant
bun add vant
```
--------------------------------
### Signature Component Installation
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/signature/README.md
Instructions on how to install and register the Vant Signature component globally.
```APIDOC
## Installation
Register component globally via `app.use`, refer to [Component Registration](#/en-US/advanced-usage#zu-jian-zhu-ce) for more registration ways.
```js
import { createApp } from 'vue';
import { Signature } from 'vant';
const app = createApp();
app.use(Signature);
```
```
--------------------------------
### Install @vant/use Package
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/vant-use-intro.zh-CN.md
Install the @vant/use package using your preferred package manager. This package contains Vant's composition APIs.
```shell
# with npm
npm i @vant/use
# with yarn
yarn add @vant/use
# with pnpm
pnpm add @vant/use
# with Bun
bun add @vant/use
```
--------------------------------
### Install Vant 4 and Compatibility Package
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/migrate-from-v3.en-US.md
Commands to install Vant 4 and the @vant/compat package using various package managers. This is the first step in the migration process.
```bash
# Install via npm
npm add vant@^4 @vant/compat@^1
# Install via yarn
yarn add vant@^4 @vant/compat@^1
# Install via pnpm
pnpm add vant@^4 @vant/compat@^1
# Install via Bun
bun add vant@^4 @vant/compat@^1
```
--------------------------------
### SwipeCell Installation
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/swipe-cell/README.md
Instructions on how to install and register the SwipeCell component globally.
```APIDOC
## SwipeCell Installation
Register component globally via `app.use`, refer to [Component Registration](#/en-US/advanced-usage#zu-jian-zhu-ce) for more registration ways.
```js
import { createApp } from 'vue';
import { SwipeCell } from 'vant';
const app = createApp();
app.use(SwipeCell);
```
```
--------------------------------
### Install Vant Touch Emulator
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/advanced-usage.zh-CN.md
Install the `@vant/touch-emulator` package using npm. This package enables mouse event simulation for touch devices, allowing Vant components to work on desktop browsers.
```bash
# 安装模块
npm i @vant/touch-emulator -S
```
--------------------------------
### Install @vant/popperjs with yarn
Source: https://github.com/youzan/vant/blob/main/packages/vant-popperjs/README.md
Use this command to install the package with yarn.
```shell
yarn add @vant/popperjs
```
--------------------------------
### Install Vant Area Data via pnpm
Source: https://github.com/youzan/vant/blob/main/packages/vant-area-data/README.md
Install the package using pnpm.
```shell
pnpm add @vant/area-data
```
--------------------------------
### Install @vant/popperjs with npm
Source: https://github.com/youzan/vant/blob/main/packages/vant-popperjs/README.md
Use this command to install the package with npm.
```shell
npm i @vant/popperjs
```
--------------------------------
### Use Vant Button in
```
--------------------------------
### Install Vant Icons with Bun
Source: https://github.com/youzan/vant/blob/main/packages/vant-icons/README.md
Use this command to install Vant Icons if you are using Bun as your package manager.
```shell
# with Bun
bun add @vant/icons
```
--------------------------------
### Install Plugins for On-Demand CSS Import
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/quickstart.zh-CN.md
Install necessary plugins for automatic component import and CSS styling with Vant, compatible with Rsbuild, Vite, webpack, or vue-cli.
```bash
# 通过 npm 安装
npm i @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
# 通过 yarn 安装
yarn add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
# 通过 pnpm 安装
pnpm add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
# 通过 bun 安装
bun add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
```
--------------------------------
### Install Vant Area Data via npm
Source: https://github.com/youzan/vant/blob/main/packages/vant-area-data/README.md
Install the package using npm.
```shell
npm i @vant/area-data
```
--------------------------------
### Install @vant/popperjs with pnpm
Source: https://github.com/youzan/vant/blob/main/packages/vant-popperjs/README.md
Use this command to install the package with pnpm.
```shell
pnpm add @vant/popperjs
```
--------------------------------
### Install Stylelint Dependencies
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/release-note-v4.zh-CN.md
Vant Cli 5.0 no longer installs stylelint and its configuration by default. Install them manually if needed.
```bash
npm add stylelint@13 @vant/stylelint-config
```
--------------------------------
### Install Vant Area Data via yarn
Source: https://github.com/youzan/vant/blob/main/packages/vant-area-data/README.md
Install the package using yarn.
```shell
yarn add @vant/area-data
```
--------------------------------
### Install Vant with npm
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/quickstart.zh-CN.md
Use npm to install the latest Vant for Vue 3 projects or Vant 2 for Vue 2 projects.
```bash
npm i vant
npm i vant@latest-v2
```
--------------------------------
### Layout Installation
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/col/README.md
Install and register the Row and Col components globally using app.use.
```APIDOC
## Installation
### Description
Register the Row and Col components globally using `app.use`.
### Code
```javascript
import { createApp } from 'vue';
import { Col, Row } from 'vant';
const app = createApp();
app.use(Col);
app.use(Row);
```
```
--------------------------------
### Install @vant/compat package
Source: https://github.com/youzan/vant/blob/main/packages/vant-compat/README.md
Commands to install the @vant/compat compatibility layer using various package managers including npm, yarn, pnpm, and bun.
```shell
# with npm
npm i @vant/compat
# with yarn
yarn add @vant/compat
# with pnpm
pnpm add @vant/compat
# with Bun
bun add @vant/compat
```
--------------------------------
### Installation and Basic Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/steps/README.zh-CN.md
How to import and use the Steps and Step components in a Vue application.
```APIDOC
## Installation and Basic Usage
### Introduction
Used to display the various stages of an operation process, allowing users to understand their current position in the overall flow.
### Importing Components
Globally register the components using the following method. For more registration methods, please refer to [Component Registration](#/zh-CN/advanced-usage#zu-jian-zhu-ce).
```js
import { createApp } from 'vue';
import { Step, Steps } from 'vant';
const app = createApp();
app.use(Step);
app.use(Steps);
```
## Code Demonstration
### Basic Usage
The `active` property indicates the index of the current step, starting from 0.
```html
Buyer Places OrderSeller Accepts OrderBuyer Picks Up GoodsTransaction Complete
```
```js
import { ref } from 'vue';
export default {
setup() {
const active = ref(1);
return { active };
},
};
```
```
--------------------------------
### Get Uploader Instance Type
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/uploader/README.zh-CN.md
Demonstrates how to get the type for the Uploader component instance. This is useful for referencing the component instance in your Vue setup.
```typescript
import { ref } from 'vue';
import type { UploaderInstance } from 'vant';
const uploaderRef = ref();
uploaderRef.value?.chooseFile();
```
--------------------------------
### Vue Component Setup for ImagePreview
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/image-preview/README.zh-CN.md
Provides the necessary Vue 3 Composition API setup for the `van-image-preview` component, including reactive state for visibility, current index, and image sources.
```javascript
import { ref } from 'vue';
export default {
setup() {
const show = ref(false);
const index = ref(0);
const images = [
'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
];
const onChange = (newIndex) => {
index.value = newIndex;
};
return {
show,
index,
images,
onChange,
};
},
};
```
--------------------------------
### Ellipsis at Start - TextEllipsis
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/text-ellipsis/README.zh-CN.md
Control the ellipsis position to appear at the beginning of the text using `position="start"`. This example truncates after 1 row.
```html
```
```javascript
export default {
setup() {
const text =
'那一天我二十一岁,在我一生的黄金时代。我有好多奢望。我想爱,想吃,还想在一瞬间变成天上半明半暗的云。后来我才知道,生活就是个缓慢受锤的过程,人一天天老下去,奢望也一天天消失,最后变得像挨了锤的牛一样。可是我过二十一岁生日时没有预见到这一点。我觉得自己会永远生猛下去,什么也锤不了我。';
return { text };
},
};
```
--------------------------------
### Installation and Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/watermark/README.zh-CN.md
Instructions on how to import and register the Vant Watermark component globally.
```APIDOC
## Installation and Usage
### Introduction
Add specific text or patterns as watermarks on the page to prevent information theft. Please upgrade `vant` to version >= 4.2.0 to use this component.
### Import
Register the component globally as follows. For more registration methods, please refer to [Component Registration](#/zh-CN/advanced-usage#zu-jian-zhu-ce).
```js
import { createApp } from 'vue';
import { Watermark } from 'vant';
const app = createApp();
app.use(Watermark);
```
```
--------------------------------
### Install Vant Rate Component Globally
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/rate/README.md
Demonstrates how to install the Vant Rate component globally using app.use in a Vue application. This is a common setup for making components readily available throughout the app.
```javascript
import { createApp } from 'vue';
import { Rate } from 'vant';
const app = createApp();
app.use(Rate);
```
--------------------------------
### Quickstart Component Registration
Source: https://github.com/youzan/vant/blob/main/README.md
Demonstrates how to import specific Vant components and their corresponding styles into a Vue 3 application, followed by global registration.
```javascript
import { createApp } from 'vue';
// 1. Import the components you need
import { Button } from 'vant';
// 2. Import the components style
import 'vant/lib/index.css';
const app = createApp();
// 3. Register the components you need
app.use(Button);
```
--------------------------------
### Basic Usage Example
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/use-relation.zh-CN.md
Demonstrates how to establish a parent-child relationship using `useChildren` in the parent and `useParent` in the child.
```APIDOC
## Basic Usage
### Parent Component (`useChildren`)
In the parent component, use `useChildren` to associate child components:
```js
import { ref } from 'vue';
import { useChildren } from '@vant/use';
const RELATION_KEY = Symbol('my-relation');
export default {
setup() {
const { linkChildren } = useChildren(RELATION_KEY);
const count = ref(0);
const add = () => {
count.value++;
};
// Provide data and methods to child components
linkChildren({ add, count });
},
};
```
### Child Component (`useParent`)
In the child component, use `useParent` to get the data and methods provided by the parent:
```js
import { useParent } from '@vant/use';
export default {
setup() {
const { parent } = useParent(RELATION_KEY);
// Call methods and access data provided by the parent
if (parent) {
parent.add();
console.log(parent.count.value); // -> 1
}
},
};
```
```
--------------------------------
### Using RollingTextInstance Type in Vue
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/rolling-text/README.zh-CN.md
Type a template ref with `RollingTextInstance` to get proper autocompletion and type checking when accessing component methods like `start`.
```typescript
import { ref } from 'vue';
import type { RollingTextInstance } from 'vant';
const rollingTextRef = ref();
rollingTextRef.value?.start();
```
--------------------------------
### Create Vant CLI Project
Source: https://github.com/youzan/vant/blob/main/packages/vant-cli/README.md
Run this command to initialize a new Vant CLI project.
```bash
yarn create vant-cli-app
```
--------------------------------
### Vant ActionBar with Icon Badges (Vue.js)
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/action-bar/README.md
This example demonstrates how to use the `dot` and `badge` props on ActionBarIcon components to display notification badges. It includes examples for a simple red dot and a badge with a numerical count. Assumes Vant is installed and components are registered.
```html
```
--------------------------------
### Vue Component Setup for ImagePreview with Video Slot
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/image-preview/README.zh-CN.md
Vue 3 Composition API setup for using the `van-image-preview` component with a custom `image` slot that renders a video player. Includes reactive state for visibility and image sources.
```javascript
import { ref } from 'vue';
export default {
setup() {
const show = ref(false);
const images = [
'https://www.w3school.com.cn/i/movie.ogg',
'https://www.w3school.com.cn/i/movie.ogg',
'https://www.w3school.com.cn/i/movie.ogg',
];
return {
show,
images,
};
},
};
```
--------------------------------
### Vue Setup for Manual RollingText Control
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/rolling-text/README.zh-CN.md
Implement manual control functions for starting and resetting the RollingText animation. Access the component instance using a template ref.
```javascript
import { ref } from 'vue';
export default {
setup() {
const rollingTextRef = ref(null);
const start = () => {
rollingTextRef.value.start();
};
const reset = () => {
rollingTextRef.value.reset();
};
return { rollingTextRef, start, reset };
},
};
```
--------------------------------
### Configure Circle Appearance and Position
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/circle/README.md
Examples of customizing the Circle component using props for layer color, direction, size, and starting position.
```html
```
--------------------------------
### Basic Usage of useScrollParent
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/use-scroll-parent.zh-CN.md
This example demonstrates how to use the useScrollParent hook to get the scrollable parent of a div element and attach a scroll event listener to it. Ensure the target element is rendered in the DOM.
```html
```
```javascript
import { ref, watch } from 'vue';
import { useScrollParent, useEventListener } from '@vant/use';
export default {
setup() {
const root = ref();
const scrollParent = useScrollParent(root);
useEventListener(
'scroll',
() => {
console.log('scroll');
},
{ target: scrollParent },
);
return { root };
},
};
```
--------------------------------
### Vant Tabs with Swipeable and Sticky Element
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/tab/README.zh-CN.md
When `swipeable` or `animated` is enabled on `van-tabs`, content elements wrapped in `van-sticky` may not display as expected due to transform positioning. This example demonstrates the setup where the issue occurs.
```html
sticky button
```
--------------------------------
### Configure Auto-Import Plugins for Build Tools
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/quickstart.en-US.md
Configures Vant auto-import resolvers for Rsbuild, Vite, Vue-CLI, and Webpack. These configurations use unplugin-auto-import and unplugin-vue-components to automate component registration and style loading.
```js
import { defineConfig } from '@rsbuild/core';
import { pluginVue } from '@rsbuild/plugin-vue';
import AutoImport from 'unplugin-auto-import/rspack';
import Components from 'unplugin-vue-components/rspack';
import { VantResolver } from '@vant/auto-import-resolver';
export default defineConfig({
plugins: [pluginVue()],
tools: {
rspack: {
plugins: [
AutoImport({ resolvers: [VantResolver()] }),
Components({ resolvers: [VantResolver()] }),
],
},
},
});
```
```js
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
export default {
plugins: [
vue(),
AutoImport({ resolvers: [VantResolver()] }),
Components({ resolvers: [VantResolver()] }),
],
};
```
```js
const { VantResolver } = require('@vant/auto-import-resolver');
const AutoImport = require('unplugin-auto-import/webpack');
const Components = require('unplugin-vue-components/webpack');
module.exports = {
configureWebpack: {
plugins: [
AutoImport.default({ resolvers: [VantResolver()] }),
Components.default({ resolvers: [VantResolver()] }),
],
},
};
```
--------------------------------
### Enable PC Browser Touch Emulation
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/advanced-usage.en-US.md
Installs and imports the touch emulator module to convert mouse events into touch events, allowing mobile-first Vant components to function correctly on desktop browsers.
```bash
npm i @vant/touch-emulator -S
```
```javascript
import '@vant/touch-emulator';
```
--------------------------------
### Vant Collapse toggleAll Method Examples
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/collapse/README.zh-CN.md
Demonstrates how to use the toggleAll method to control the expanded state of all collapse items. Supports options like skipping disabled items.
```javascript
import { ref } from 'vue';
import type { CollapseInstance } from 'vant';
const collapseRef = ref();
// 全部切换
collapseRef.value?.toggleAll();
// 全部展开
collapseRef.value?.toggleAll(true);
// 全部收起
collapseRef.value?.toggleAll(false);
// 全部全部切换,并跳过禁用的复选框
collapseRef.value?.toggleAll({
skipDisabled: true,
});
// 全部选中,并跳过禁用的复选框
collapseRef.value?.toggleAll({
expanded: true,
skipDisabled: true,
});
```
--------------------------------
### Basic Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/loading/README.zh-CN.md
Demonstrates the basic usage of the Loading component with default settings.
```APIDOC
## Basic Usage
### Description
Displays a circular loading indicator.
### Method
N/A (Component Usage)
### Endpoint
N/A (Component Usage)
### Request Example
```html
```
### Response Example
(No direct response, renders a loading spinner)
```
--------------------------------
### Vant ActionBar with Custom Button Colors (Vue.js)
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/action-bar/README.md
This example demonstrates how to customize the background color of ActionBarButton components using the `color` prop. It supports single colors and linear gradients. Assumes Vant is installed and components are registered.
```html
```
--------------------------------
### Basic Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/address-list/README.zh-CN.md
Demonstrates the basic implementation of the AddressList component with sample data.
```APIDOC
## Basic Usage
### Introduction
Displays a list of addresses.
### Import
Globally register the component using the following method. For more registration methods, please refer to [Component Registration](#/zh-CN/advanced-usage#zu-jian-zhu-ce).
```js
import { createApp } from 'vue';
import { AddressList } from 'vant';
const app = createApp();
app.use(AddressList);
```
### Code Demonstration
```html
```
```js
import { ref } from 'vue';
import { showToast } from 'vant';
export default {
setup() {
const chosenAddressId = ref('1');
const list = [
{
id: '1',
name: '张三',
tel: '13000000000',
address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
isDefault: true,
},
{
id: '2',
name: '李四',
tel: '1310000000',
address: '浙江省杭州市拱墅区莫干山路 50 号',
},
];
const disabledList = [
{
id: '3',
name: '王五',
tel: '1320000000',
address: '浙江省杭州市滨江区江南大道 15 号',
},
];
const onAdd = () => showToast('新增地址');
const onEdit = (item, index) => showToast('编辑地址:' + index);
return {
list,
onAdd,
onEdit,
disabledList,
chosenAddressId,
};
},
};
```
```
--------------------------------
### Register Vant Components in Vue
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/advanced-usage.en-US.md
Demonstrates various ways to register Vant components, including global registration via app.use, full library registration, local component registration, and usage within script setup or JSX.
```javascript
import { Button } from 'vant';
import { createApp } from 'vue';
const app = createApp();
// Method 1. via app.use
app.use(Button);
// Method 2. Register via app.component
app.component(Button.name, Button);
```
```javascript
import Vant from 'vant';
import { createApp } from 'vue';
const app = createApp();
app.use(Vant);
// The Lazyload directive needs to be registered separately
app.use(vant.Lazyload);
```
```javascript
import { Button } from 'vant';
export default {
components: {
[Button.name]: Button,
},
};
```
```xml
```
```jsx
import { Button } from 'vant';
export default {
render() {
return ;
},
};
```
--------------------------------
### Vant Rate Component with Half Star Support
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/rate/README.md
Explains how to enable half-star selection in the Vant Rate component using the 'allow-half' prop. The example shows the HTML template and the JavaScript setup for a reactive 'value' that can hold decimal values.
```html
```
```javascript
import { ref } from 'vue';
export default {
setup() {
const value = ref(2.5);
return { value };
},
};
```
--------------------------------
### Component Instance Method Call Example
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/advanced-usage.zh-CN.md
Demonstrates calling a component instance method (`toggle()`) after the component has been mounted. Ensure the ref is accessible via `this.$refs`.
```js
export default {
data() {
return {
checked: false,
};
},
// 注意:组件挂载后才能访问到 ref 对象
mounted() {
this.$refs.checkbox.toggle();
},
};
```
--------------------------------
### Vant ActionBar with Custom Icon Colors (Vue.js)
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/action-bar/README.md
This example shows how to customize the color of ActionBarIcon components using the `color` prop. It applies specific colors to individual icons while using default colors for others. Assumes Vant is installed and components are registered.
```html
```
--------------------------------
### Vue Setup for Custom Image Preview
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/uploader/README.zh-CN.md
Configure the fileList with URLs and custom properties like deletable and beforeDelete for individual image previews.
```javascript
import { ref } from 'vue';
import { showToast } from 'vant';
export default {
setup() {
const fileList = ref([
{
url: 'https://fastly.jsdelivr.net/npm/@vant/assets/sand.jpeg',
deletable: true,
beforeDelete: () => {
showToast('删除前置处理');
},
},
{
url: 'https://fastly.jsdelivr.net/npm/@vant/assets/tree.jpeg',
imageFit: 'contain',
},
]);
return { fileList };
},
};
```
--------------------------------
### Retrieve Element Dimensions and Position with useRect
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/use-rect.en-US.md
Demonstrates how to use the useRect hook within a Vue 3 setup function to get the bounding client rect of a referenced DOM element. It requires a template ref and is typically invoked within the onMounted lifecycle hook.
```html
```
```javascript
import { ref, onMounted } from 'vue';
import { useRect } from '@vant/use';
export default {
setup() {
const root = ref();
onMounted(() => {
const rect = useRect(root);
console.log(rect); // -> the size of an element and its position relative to the viewport
});
return { root };
},
};
```
--------------------------------
### Code Examples
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/watermark/README.zh-CN.md
Demonstrates various ways to use the Watermark component with different configurations.
```APIDOC
## Code Examples
### Text Watermark
Set the watermark text using the `content` attribute.
```html
```
### Image Watermark
Set the watermark image using the `image` attribute and adjust the overall opacity with `opacity`.
```html
```
### Custom Spacing
Control the spacing between multiple repeating watermarks using `gap-x` and `gap-y`.
```html
```
### Custom Rotation Angle
Control the rotation angle of the watermark using the `rotate` attribute. The default value is `-22`.
```html
```
### Display Range
Control the display range of the watermark using the `full-page` attribute.
```html
```
### HTML Watermark
Pass HTML directly as a watermark using the `content` slot. Styles in HTML only support inline styles and do not support self-closing tags.
```html
Vant watermark
```
```
--------------------------------
### Vant Rate Component Change Event Handling
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/rate/README.md
Demonstrates how to handle the 'change' event emitted by the Vant Rate component. The example shows the HTML template with the @change listener and the JavaScript setup, including a function to display a toast message with the new value.
```html
```
```javascript
import { ref } from 'vue';
import { showToast } from 'vant';
export default {
setup() {
const value = ref(3);
const onChange = (value) => showToast('current value:' + value);
return {
value,
onChange,
};
},
};
```
--------------------------------
### Basic Vant ActionBar Usage with Icons and Button (Vue.js)
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/action-bar/README.md
This example shows the basic structure of a Vant ActionBar, including ActionBarIcon and ActionBarButton components. It utilizes click event handlers to display toasts when icons or the button are pressed. Assumes Vant is installed and components are registered.
```html
```
```javascript
import { showToast } from 'vant';
export default {
setup() {
const onClickIcon = () => showToast('Click Icon');
const onClickButton = () => showToast('Click Button');
return {
onClickIcon,
onClickButton,
};
},
};
```
--------------------------------
### Basic Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/sticky/README.zh-CN.md
Demonstrates the basic implementation of the Sticky component by wrapping content within it.
```APIDOC
## Basic Usage
将内容包裹在 `Sticky` 组件内即可。
```html
基础用法
```
```
--------------------------------
### Basic Steps Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/steps/README.zh-CN.md
Demonstrates the basic usage of the Steps component. Use the `active` prop to set the current step index, starting from 0.
```html
买家下单商家接单买家提货交易完成
```
```javascript
import { ref } from 'vue';
export default {
setup() {
const active = ref(1);
return { active };
},
};
```
--------------------------------
### Basic Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/icon/README.zh-CN.md
Demonstrates the basic usage of the Icon component by specifying the icon name.
```APIDOC
## Basic Usage
### Description
Use the `name` property to specify the icon to use. Vant includes a built-in icon library.
### Method
GET
### Endpoint
N/A (Component usage)
### Request Example
```html
```
```
--------------------------------
### Dialog Installation
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/dialog/README.zh-CN.md
Global registration of the Dialog component.
```APIDOC
## Installation
### Global Registration
To globally register the component, use the following code:
```js
import { createApp } from 'vue';
import { Dialog } from 'vant';
const app = createApp();
app.use(Dialog);
```
```
--------------------------------
### Basic Usage - AddressList
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/address-list/README.zh-CN.md
Demonstrates the basic usage of the AddressList component with sample data and event handlers.
```html
```
```javascript
import { ref } from 'vue';
import { showToast } from 'vant';
export default {
setup() {
const chosenAddressId = ref('1');
const list = [
{
id: '1',
name: '张三',
tel: '13000000000',
address: '浙江省杭州市西湖区文三路 13000000000东方通信大厦 7 楼 501 室',
isDefault: true,
},
{
id: '2',
name: '李四',
tel: '1310000000',
address: '浙江省杭州市拱墅区莫干山路 50 号',
},
];
const disabledList = [
{
id: '3',
name: '王五',
tel: '1320000000',
address: '浙江省杭州市滨江区江南大道 15 号',
},
];
const onAdd = () => showToast('新增地址');
const onEdit = (item, index) => showToast('编辑地址:' + index);
return {
list,
onAdd,
onEdit,
disabledList,
chosenAddressId,
};
},
};
```
--------------------------------
### Switch Languages with Locale.use
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/locale/README.md
Use the `Locale.use` method to switch to different languages. Ensure you import the desired language pack.
```javascript
import { Locale } from 'vant';
import enUS from 'vant/es/locale/lang/en-US';
Locale.use('en-US', enUS);
```
--------------------------------
### Basic Picker Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/picker/README.zh-CN.md
Configure options using the `columns` prop. Handle user selections with `@confirm`, `@cancel`, and `@change` events. The `onConfirm` event provides selected values.
```html
```
```js
import { showToast } from 'vant';
export default {
setup() {
const columns = [
{ text: '杭州', value: 'Hangzhou' },
{ text: '宁波', value: 'Ningbo' },
{ text: '温州', value: 'Wenzhou' },
{ text: '绍兴', value: 'Shaoxing' },
{ text: '湖州', value: 'Huzhou' },
];
const onConfirm = ({ selectedValues }) => {
showToast(`当前值: ${selectedValues.join(',')}`);
};
const onChange = ({ selectedValues }) => {
showToast(`当前值: ${selectedValues.join(',')}`);
};
const onCancel = () => showToast('取消');
return {
columns,
onChange,
onCancel,
onConfirm,
};
},
};
```
--------------------------------
### CouponCell and CouponList Introduction
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/coupon-list/README.zh-CN.md
Introduces the CouponCell and CouponList components and provides instructions for global registration.
```APIDOC
## Coupon 优惠券选择器
### 介绍
用于优惠券的兑换和选择。
### 引入
通过以下方式来全局注册组件,更多注册方式请参考[组件注册](#/zh-CN/advanced-usage#zu-jian-zhu-ce)。
```js
import { createApp } from 'vue';
import { CouponCell, CouponList } from 'vant';
const app = createApp();
app.use(CouponCell);
app.use(CouponList);
```
```
--------------------------------
### Basic Vant CLI Configuration
Source: https://github.com/youzan/vant/blob/main/packages/vant-cli/docs/config.zh-CN.md
A basic `vant.config.mjs` setup for a component library, including its name, build site public path, and site metadata like title, logo, and navigation.
```javascript
export default {
// 组件库名称
name: 'demo-ui',
// 构建配置
build: {
site: {
publicPath: '/demo-ui/',
},
},
// 文档站点配置
site: {
// 标题
title: 'Demo UI',
// 图标
logo: 'https://fastly.jsdelivr.net/npm/@vant/assets/logo.png',
// 描述
description: '示例组件库',
// 左侧导航
nav: [
{
title: '开发指南',
items: [
{
path: 'home',
title: '介绍',
},
],
},
{
title: '基础组件',
items: [
{
path: 'my-button',
title: 'MyButton 按钮',
},
],
},
],
},
};
```
--------------------------------
### Install @vant/use package
Source: https://github.com/youzan/vant/blob/main/packages/vant/docs/markdown/vant-use-intro.en-US.md
Commands to install the @vant/use dependency using various package managers. It is recommended to install this package explicitly even if it is included in Vant's dependencies.
```shell
# with npm
npm i @vant/use
# with yarn
yarn add @vant/use
# with pnpm
pnpm add @vant/use
# with Bun
bun add @vant/use
```
--------------------------------
### Signature Component Usage
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/signature/README.md
Examples demonstrating basic usage, customizing pen color, line width, and background color of the Signature component.
```APIDOC
## Usage
### Basic Usage
When the confirm button is clicked, the component will emit the `submit` event. The first parameter of the event is `data`, which contains the following fields:
- `image`: The image corresponding to the signature, which is in base64 string format. Returns an empty string if the signature is empty.
- `canvas`: The Canvas element.
```html
```
```js
import { ref } from 'vue';
import { showToast } from 'vant';
export default {
setup() {
const image = ref('');
const onSubmit = (data) => {
image.value = data.image;
};
const onClear = () => showToast('clear');
return {
image,
onSubmit,
onClear,
};
},
};
```
### Pen Color
Use `pen-color` prop to set the color of the brush stroke.
```html
```
### Line Width
Use `line-width` prop to set the width of the line.
```html
```
### Background Color
Use `background-color` prop to set the color of the background.
```html
```
```
--------------------------------
### Install Vant Tabbar and TabbarItem Globally
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/tabbar/README.md
This snippet demonstrates how to install the Vant Tabbar and TabbarItem components globally using `app.use` in a Vue application. Ensure Vant is installed in your project.
```javascript
import { createApp } from 'vue';
import { Tabbar, TabbarItem } from 'vant';
const app = createApp();
app.use(Tabbar);
app.use(TabbarItem);
```
--------------------------------
### Scaffold a Project with Rsbuild
Source: https://github.com/youzan/vant/blob/main/README.md
Initialize a new project using the Rsbuild scaffold tool, which is recommended for optimal performance and integration with Vant.
```bash
npm create rsbuild@latest
```
--------------------------------
### Install Vant Collapse Component (Vue.js)
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/collapse/README.md
This snippet demonstrates how to install and register the Vant Collapse and CollapseItem components globally in a Vue.js application using `app.use`. Ensure Vant is installed in your project.
```js
import { createApp } from 'vue';
import { Collapse, CollapseItem } from 'vant';
const app = createApp();
app.use(Collapse);
app.use(CollapseItem);
```
--------------------------------
### Vue Setup for Reupload
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/uploader/README.zh-CN.md
Initialize fileList with an initial image URL when reupload functionality is enabled.
```typescript
import { ref } from 'vue';
export default {
setup() {
const fileList = ref([
{ url: 'https://fastly.jsdelivr.net/npm/@vant/assets/leaf.jpeg' },
]);
return { fileList };
},
};
```
--------------------------------
### Install ActionSheet Component Globally - JavaScript
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/action-sheet/README.md
Demonstrates how to install the Vant ActionSheet component globally using `app.use`. This is a common way to register components for use throughout a Vue application. Ensure you have Vue and Vant installed.
```javascript
import { createApp } from 'vue';
import { ActionSheet } from 'vant';
const app = createApp();
app.use(ActionSheet);
```
--------------------------------
### 全局注册 TreeSelect 组件
Source: https://github.com/youzan/vant/blob/main/packages/vant/src/tree-select/README.zh-CN.md
通过 `createApp` 和 `app.use()` 方法全局注册 TreeSelect 组件。更多注册方式请参考组件注册文档。
```javascript
import { createApp } from 'vue';
import { TreeSelect } from 'vant';
const app = createApp();
app.use(TreeSelect);
```