最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2023年10月05日 08:16:15   作者:Saga Two  
Vant是一個(gè)強(qiáng)大的移動(dòng)端組件庫,目前Vant 官方提供了 Vue 2 版本,Vue 3 版本和微信小程序版本,本文主要為大家介紹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)文章

最新評論

神农架林区| 淳安县| 曲阳县| 玛纳斯县| 遵义县| 嵊州市| 林芝县| 桃园县| 泰顺县| 遂川县| 泰和县| 翁牛特旗| 灵璧县| 霍林郭勒市| 石林| 离岛区| 马边| 三穗县| 鹤峰县| 长岭县| 樟树市| 庄河市| 松潘县| 句容市| 筠连县| 清流县| 昌江| 潞城市| 隆安县| 怀仁县| 丹巴县| 太和县| 城固县| 四子王旗| 邵东县| 陕西省| 阜平县| 灌云县| 丹寨县| 信宜市| 噶尔县|