5.2 KiB
Quickstart
Install
npm
Using npm
to install:
# install latest Vant for Vue 3 project
npm i vant
# install Vant 2 for Vue 2 project
npm i vant@latest-v2
Using yarn
or pnpm
:
# with yarn
yarn add vant
# with pnpm
pnpm add vant
CDN
The easiest way to use Vant is to include a CDN link in the HTML file, after which you can access all components via the global variable vant
.
<!-- import style -->
<link
rel="stylesheet"
href="https://fastly.jsdelivr.net/npm/vant@3/lib/index.css"
/>
<!-- import script -->
<script src="https://fastly.jsdelivr.net/npm/vue@3"></script>
<script src="https://fastly.jsdelivr.net/npm/vant@3/lib/vant.min.js"></script>
<script>
// Render the Button component
const app = Vue.createApp({
template: `<van-button>Button</van-button>`,
});
app.use(vant);
// Register Lazyload directive
app.use(vant.Lazyload);
// Call function component
vant.Toast('Message');
app.mount('#app');
</script>
Free CDN
You can use Vant through these free CDN services:
Note: Free CDN is generally used for making prototypes or personal projects. It is not recommended to use free CDN in production environment.
For enterprise developers, we recommend:
- install with npm, use build tools to bundle it
- download the scripts, host it on your own server
CLI
We recommend to use Vue CLI to create a new project.
# Install Vue CLI
npm install -g @vue/cli
# Create a project
vue create hello-world
# Open GUI
vue ui
In the GUI, click on 'Dependencies' -> Install Dependencies
and add vant
to the dependencies.
Usage
Basic Usage
The basic usage of Vant components;
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);
Tip: Vant supports Tree Shaking by default, so you don't need to configure any plugins, the unused JS code will be removed by Tree Shaking, but CSS styles cannot be optimized by it.
Import on demand
If you are using vite, webpack or vue-cli, you can use unplugin-vue-components, this plugin can help you to auto importing components and reduce CSS file size.
1. Install Plugin
# with npm
npm i unplugin-vue-components -D
# with yarn
yarn add unplugin-vue-components -D
# with pnpm
pnpm add unplugin-vue-components -D
2. Configure Plugin
For vite
based project,configure the plugin in the vite.config.js
file:
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
export default {
plugins: [
vue(),
Components({
resolvers: [VantResolver()],
}),
],
};
For vue-cli
based project,configure the plugin in the vue.config.js
file:
const { VantResolver } = require('unplugin-vue-components/resolvers');
const ComponentsPlugin = require('unplugin-vue-components/webpack');
module.exports = {
configureWebpack: {
plugins: [
ComponentsPlugin({
resolvers: [VantResolver()],
}),
],
},
};
For webpack
based project,configure the plugin in the webpack.config.js
file:
const { VantResolver } = require('unplugin-vue-components/resolvers');
const ComponentsPlugin = require('unplugin-vue-components/webpack');
module.exports = {
plugins: [
ComponentsPlugin({
resolvers: [VantResolver()],
}),
],
};
3. Using Components
Then you can using components from Vant in the template, the unplugin-vue-components
will automatically import the corresponding Vant components.
<template>
<van-button type="primary" />
</template>
4. Style of Function Components
Some components of Vant are provided as function, including Toast
, Dialog
, Notify
and ImagePreview
. When using function components, unplugin-vue-components
can not auto import the component style, so we need to import style manually.
// Toast
import { Toast } from 'vant';
import 'vant/es/toast/style';
// Dialog
import { Dialog } from 'vant';
import 'vant/es/dialog/style';
// Notify
import { Notify } from 'vant';
import 'vant/es/notify/style';
// ImagePreview
import { ImagePreview } from 'vant';
import 'vant/es/image-preview/style';
Tip: "Full Import" and "On-demand Import" should not be used at the same time, otherwise it will lead to problems such as code duplication and style overrides.
With Frameworks
Use Vant in Nuxt 3
When using Vant in Nuxt 3, you should add /vant/
to the build.transpile
:
import { defineNuxtConfig } from 'nuxt';
export default defineNuxtConfig({
build: {
transpile: [/vant/],
},
});
Reference: