欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3中引入使用vant組件的教程詳解

 更新時(shí)間:2023年10月05日 08:16:15   作者:Saga Two  
Vant是一個(gè)強(qiáng)大的移動(dòng)端組件庫(kù),目前Vant 官方提供了 Vue 2 版本,Vue 3 版本和微信小程序版本,本文主要為大家介紹vue3中的vant組件引入使用的方法,希望對(duì)大家有所幫助

Vant是一個(gè)強(qiáng)大的移動(dòng)端組件庫(kù),目前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)行按需引入。以下我們對(duì)兩種引入方式進(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. 注冊(cè)你需要的組件
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)注冊(cè)對(duì)應(yīng)的組件, @vant/auto-import-resolver 會(huì)自動(dòng)引入對(duì)應(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ā)效率,詳情請(qǐng)見官網(wǎng):Vant

注:在vue3中,由于vite打包擁有良好的性能,本文使用的示例為vite打包方式,同時(shí)建議使用其他包最新的支持版本進(jìn)行開發(fā)。

到此這篇關(guān)于Vue3中引入使用vant組件的教程詳解的文章就介紹到這了,更多相關(guān)Vue3 vant內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue樣式穿透 ::v-deep的具體使用

    vue樣式穿透 ::v-deep的具體使用

    這篇文章主要介紹了vue樣式穿透 ::v-deep的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Vue實(shí)現(xiàn)dom元素拖拽并限制移動(dòng)范圍的操作代碼

    Vue實(shí)現(xiàn)dom元素拖拽并限制移動(dòng)范圍的操作代碼

    這篇文章主要介紹了Vue實(shí)現(xiàn)dom元素拖拽并限制移動(dòng)范圍的操作代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-12-12
  • Vue3哈希模式實(shí)現(xiàn)錨點(diǎn)導(dǎo)航方式

    Vue3哈希模式實(shí)現(xiàn)錨點(diǎn)導(dǎo)航方式

    這篇文章主要介紹了Vue3哈希模式實(shí)現(xiàn)錨點(diǎn)導(dǎo)航方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • VUE中如何動(dòng)態(tài)綁定類名和樣式

    VUE中如何動(dòng)態(tài)綁定類名和樣式

    這篇文章主要介紹了VUE中如何動(dòng)態(tài)綁定類名和樣式問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 解析Vue 2.5的Diff算法

    解析Vue 2.5的Diff算法

    本文將對(duì)于Vue 2.5.3版本中使用的Virtual Dom進(jìn)行分析。updataChildren是Diff算法的核心,所以本文對(duì)updataChildren進(jìn)行了圖文的分析。下面通過本文給大家分享Vue 2.5的Diff算法,需要的朋友參考下吧
    2017-11-11
  • Vue 按鍵修飾符處理事件的方法

    Vue 按鍵修飾符處理事件的方法

    這篇文章主要介紹了Vue 按鍵修飾符的相關(guān)資料,vue中新增按鍵修飾符和系統(tǒng)修飾符來處理類似的事件,具體內(nèi)容詳情大家參考下本文
    2018-05-05
  • 在IDEA中Debug調(diào)試VUE項(xiàng)目的詳細(xì)步驟

    在IDEA中Debug調(diào)試VUE項(xiàng)目的詳細(xì)步驟

    idea竟然有一個(gè)神功能很多朋友都不是特別清楚,下面小編給大家?guī)砹嗽贗DEA中Debug調(diào)試VUE項(xiàng)目的詳細(xì)步驟,感興趣的朋友一起看看吧
    2021-10-10
  • Vue中的$set的使用實(shí)例代碼

    Vue中的$set的使用實(shí)例代碼

    這篇文章主要介紹了Vue中的$set的使用,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-10-10
  • 詳解 vue better-scroll滾動(dòng)插件排坑

    詳解 vue better-scroll滾動(dòng)插件排坑

    本篇文章主要介紹了詳解 vue better-scroll滾動(dòng)插件排坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • vue-cli4如何打包靜態(tài)資源到指定目錄

    vue-cli4如何打包靜態(tài)資源到指定目錄

    這篇文章主要介紹了vue-cli4打包靜態(tài)資源到指定目錄方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評(píng)論