Vue3中引入使用vant組件的教程詳解
Vant是一個(gè)強(qiáng)大的移動(dòng)端組件庫,目前Vant 官方提供了 Vue 2 版本、Vue 3 版本和微信小程序版本。本文主要介紹vue3中的vant組件引入使用。
1.安裝
vue3中使用如下命令通過 npm 安裝(本人項(xiàng)目使用的安裝方式)
# Vue 3 項(xiàng)目,安裝最新版 Vant npm i vant
也可以使用其他的包管理起進(jìn)行安裝:
# 通過 yarn 安裝 yarn add vant # 通過 pnpm 安裝 pnpm add vant # 通過 Bun 安裝 bun add vant
2.引入
Vant分為全局引入和按需引入兩種方式,一般在工程項(xiàng)目中,由于全局引入會(huì)導(dǎo)致不必要的資源加載,為提升項(xiàng)目性能,建議進(jìn)行按需引入。以下我們對兩種引入方式進(jìn)行介紹。
2.1 全局引入
全局引入就是在項(xiàng)目入口(main.ts)文件直接引入組件以及組件全部的樣式文件;代碼如下所示:
// main.ts
import { createApp } from 'vue';
// 1. 引入你需要的組件
import { Button } from 'vant';
// 2. 引入組件樣式
import 'vant/lib/index.css';
const app = createApp();
// 3. 注冊你需要的組件
app.use(Button);
app.mount('#app')2.2 按需引入
在vue3中按需引入Vant,需要使用其他的插件輔助,需要安裝自動(dòng)引入組件插件unplugin-vue-components 和Vant 官方提供的 自動(dòng)導(dǎo)入樣式的解析器 @vant/auto-import-resolver這兩款插件;安裝方法如下:
npm install -D unplugin-vue-components @vant/auto-import-resolver
然后再vite或者webpack配置中添加相應(yīng)的配置,如下所示:
2.2.1 vite項(xiàng)目:vite.config.js
// vite.config.ts
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from '@vant/auto-import-resolver';
export default defineConfig({
// ...
plugins: [
// ...
AutoImport({
resolvers: [VantResolver()],
}),
Components({
resolvers: [VantResolver()],
}),
],
})2.2.2 Webpack項(xiàng)目:webpack.config.js
// webpack.config.js
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { VantResolver } = require('@vant/auto-import-resolver');
module.exports = {
// ...
plugins: [
AutoImport({
resolvers: [VantResolver()],
}),
Components({
resolvers: [VantResolver()],
}),
],
}2.2.3 配置在vue.config.js中
導(dǎo)入方法相同:
const { defineConfig } = require('@vue/cli-service')
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { VantResolver } = require('@vant/auto-import-resolver');
module.exports = defineConfig({
configureWebpack: {
plugins: [
AutoImport({
resolvers: [VantResolver()],
}),
Components({
resolvers: [VantResolver()],
}),
],
}
})3.使用
引入完畢之后,unplugin-vue-components 會(huì)解析模板并自動(dòng)注冊對應(yīng)的組件, @vant/auto-import-resolver 會(huì)自動(dòng)引入對應(yīng)的組件樣式。我們可進(jìn)行按需引入需要使用的組件,使用方法如下,引入input組件和button組件
<template>
<div>
<label>vant示例</label>
<van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
<van-swipe-item>vant-swipe</van-swipe-item>
<van-swipe-item class="dif">2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>
</div>
</template>
<style>
.my-swipe .van-swipe-item {
color: #fff;
font-size: 20px;
line-height: 150px;
text-align: center;
background-color: #39a9ed;
}
.my-swipe .dif {
background-color: #ccdba3;
}
</style>效果如下:

此外Vant中還有其他組件,基本能滿足開發(fā)需求,提升開發(fā)效率,詳情請見官網(wǎng):Vant
注:在vue3中,由于vite打包擁有良好的性能,本文使用的示例為vite打包方式,同時(shí)建議使用其他包最新的支持版本進(jìn)行開發(fā)。
到此這篇關(guān)于Vue3中引入使用vant組件的教程詳解的文章就介紹到這了,更多相關(guān)Vue3 vant內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)dom元素拖拽并限制移動(dòng)范圍的操作代碼
這篇文章主要介紹了Vue實(shí)現(xiàn)dom元素拖拽并限制移動(dòng)范圍的操作代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
Vue3哈希模式實(shí)現(xiàn)錨點(diǎn)導(dǎo)航方式
這篇文章主要介紹了Vue3哈希模式實(shí)現(xiàn)錨點(diǎn)導(dǎo)航方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
在IDEA中Debug調(diào)試VUE項(xiàng)目的詳細(xì)步驟
idea竟然有一個(gè)神功能很多朋友都不是特別清楚,下面小編給大家?guī)砹嗽贗DEA中Debug調(diào)試VUE項(xiàng)目的詳細(xì)步驟,感興趣的朋友一起看看吧2021-10-10
詳解 vue better-scroll滾動(dòng)插件排坑
本篇文章主要介紹了詳解 vue better-scroll滾動(dòng)插件排坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02

