vue中van-picker的選項插槽的使用
van-picker的內(nèi)部選項怎么來自定義
首先要確保 Vant UI 的版本在2.10.0以上
然后利用插槽slot來實現(xiàn),在Vant 里插槽有一個進階用法 #
<van-picker
? :show-toolbar="false"
? :default-index="2" // 默認選中第三行,選中第一行的話,上面會有一段空白,不好看
? :loading="loading"
? :columns="columns" // 一定要綁定數(shù)據(jù)源
>
? <template #option="item"> // 這里的item就是每一個選項,可以是一個對象也可以是一個字符串
? // 我這里實現(xiàn)的是每一行的選項由id和name組成
? // 切記不要用v-for,會導(dǎo)致數(shù)據(jù)重復(fù)出現(xiàn)在一個選項里
? ? <van-row>
? ? ? <van-col :span="12" class="van-hairline--right">
? ? ? ? <p style="text-align: center">
? ? ? ? ? {{ item.id }}
? ? ? ? </p>
? ? ? </van-col>
? ? ? <van-col :span="12" class="van-hairline--left">
? ? ? ? <p style="text-align: center">
? ? ? ? ? {{ item.name }}
? ? ? ? </p>
? ? ? </van-col>
? ? </van-row>
? </template>
</van-picker>附上Vant的官方地址:https://vant-contrib.gitee.io/vant/#/zh-CN/home
Vant選擇器使用插槽

官方提供樣式滿足不了自己需要的樣式時可以使用插槽可以自定義布局樣式:
我這里配合了popup彈窗層使用,具體看自己的需求
<van-popup v-model="sexShow" position="bottom" :style="{ height: '50%' }">
<van-picker :show-toolbar="true" :columns="columns" ref="getValues" @confirm="onConfirm">
<template #cancel="item">
<p>
性別
</p>
</template>
<template #confirm="item">
<div class="sexContent">
<!-- <van-button class="submit">保存</van-button> -->
<van-button class="submited">保存</van-button>
</div>
</template>
<template #option="item">
<p style="text-align: center">
{{item.text}}
</p>
</template>
</van-picker>
</van-popup>
columns: [{
text: '男',
value: 1
},
{
text: '女',
value: 2
}],
方法:
onConfirm(value) {
console.log(value) //打印的是自己定義的數(shù)組對象,然后就可以進行自己的相關(guān)操作了
},
另外:
this.$refs.getValues.getValues()
是通過 ref 可以獲取到 Picker 實例并調(diào)用實例方法,不知道的話可以自己先打印出來看看,
雖然簡單,但是也是第一次遇到,記錄一下下,以免忘記(我的記憶只有七秒,忘得快)
下面是效果圖,有點丑,勿噴

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中動態(tài)添加style樣式的幾種寫法總結(jié)
這篇文章主要介紹了vue中動態(tài)添加style樣式的幾種寫法總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

