Vant: Mobile UI Components for Vue
Vant is a lightweight and highly customizable UI component library designed for mobile web applications built with Vue.js. The current stable version series is 4.x, specifically 4.9.24 for Vue 3 projects, while Vant 2.x (e.g., 2.13.9) continues to support Vue 2. The library maintains an active release cadence, frequently adding new features, bug fixes, and improvements within its major versions. Key differentiators include its small bundle size (1KB average per component min+gzip), over 80 high-quality components, zero third-party dependencies, strong TypeScript support, extensive documentation, and comprehensive feature set including tree-shaking, custom themes, accessibility, dark mode, SSR, and i18n with 30+ built-in languages. It also provides dedicated modules for Nuxt 2 and Nuxt 3.
Common errors
-
Error: Failed to resolve component: van-button
cause Vant component was used in a Vue template but not registered with the Vue app.fixEnsure all Vant components used in templates are explicitly registered with your Vue application instance via `app.use(ComponentName);` or are automatically imported through a build tool plugin. -
TypeError: Cannot read properties of undefined (reading 'use')
cause Attempting to use `app.use()` on a Vant component that is not a plugin or trying to access a method on an undefined Vant export.fixVerify the correct import path and ensure the symbol being used is indeed a Vue plugin (e.g., `Button` is not a plugin directly, but a component. `Toast` or `Dialog` can be installed as plugins or used via their API methods after importing their functions). -
Uncaught SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in an environment that expects CommonJS (e.g., Node.js script without `--experimental-modules` or misconfigured Webpack/Rollup).fixEnsure your project is configured for ES modules, typically by setting `"type": "module"` in `package.json` or by using a modern bundler like Vite/Webpack configured for ESM output. If targeting an older Node.js environment, consider a transpilation step.
Warnings
- breaking Vant 3.x and 4.x are designed for Vue 3. Migrating from Vant 2.x (Vue 2) to Vant 3.x/4.x requires upgrading your Vue project to Vue 3, which involves significant breaking changes in Vue itself.
- gotcha Forgetting to import the global Vant CSS file (`vant/lib/index.css`) will result in components rendering without any styling, appearing as plain HTML elements.
- gotcha When using on-demand import (e.g., with `unplugin-vue-components` or `vite-plugin-components`), ensure the Vant resolver is correctly configured, otherwise components might not be automatically imported or their styles may be missing.
- deprecated Older versions of Vant might have exposed components directly via `vant/es/{componentName}`, but the recommended and tree-shakable way is to use named imports from the main `vant` package.
- gotcha Vant components are designed for mobile environments. Using them directly in desktop browsers might lead to unexpected layout or interaction issues without `vant-touch-emulator` or proper desktop styling.
Install
-
npm install vant -
yarn add vant -
pnpm add vant
Imports
- Button
import Button from 'vant/es/button'; // Older, less common, or direct import if not using tree-shaking setup const Button = require('vant').Button; // CommonJS is not the primary way for Vue components, especially with tree-shakingimport { Button } from 'vant'; - CSS Styles
import 'vant/es/index.css'; // Incorrect path for modern Vant 4 import 'vant/lib/index.less'; // If using Less, requires pre-processing import 'vant/lib/index.scss'; // If using Sass, requires pre-processing
import 'vant/lib/index.css';
- Locale
import { useLocale } from 'vant'; // Incorrect hook name for global locale configimport { Locale } from 'vant'; Locale.use('en-US');
Quickstart
import { createApp } from 'vue';
import { Button, Toast } from 'vant';
import 'vant/lib/index.css';
const app = createApp({
template: `
<van-button type="primary" @click="showToast">Click me</van-button>
`,
methods: {
showToast() {
Toast('Hello Vant!');
}
}
});
app.use(Button);
app.use(Toast);
app.mount('#app');