本文主要是介绍vue3+vant4中表单内嵌picker的默认值设置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
vue3+vant4中表单内嵌picker的默认值设置
头一次用vant就在表单默认值上费劲了,搞下来代码量比antd系列的代码量大。废话不说,直接上代码,将这三段代码直接复制到一个组件中即可看到效果
环境版本
“vant”: “^4.9.1”,
“vue”: “^3.4.21”
<script setup lang="ts">
import { ref } from 'vue'
const 地支 = '子丑寅卯辰巳午未申酉戌亥'// 用于表单下picker的v-model的值必须是字符串,数字不行,而在单独的picker里可以
const years = Array(100).fill(0).map((_, index) => new Date().getFullYear() - index).map(item => ({ text: item + '' })).reverse()
const year = ref(['1980'])
const months = Array(12).fill(0).map((_, index) => index + 1).map(item => ({ text: item + '' }))
const month = ref(['6'])
const dates = Array(30).fill(0).map((_, index) => index + 1).map(item => ({ text: item + '' }))
const date = ref(['15'])
const 时辰们 = Array(12).fill(0).map((_, index) => index + 1).map(item => {const text = 地支[item - 1] + '——' + (item === 1 ? '23点(昨)~1点' : `${item * 2 - 3}点~${item * 2 - 1}点`)return { text }
})
const 时辰 = ref([时辰们[5].text])const showYearPicker = ref(false)
const showMonthPicker = ref(false)
const showDatePicker = ref(false)
const show时辰Picker = ref(false)const info = ref(null)const 提交表单 = (values) => {info.value = valuesconsole.log('submit', values);
}
</script>
<template><div class='p-home'><h3>请输入农历出生信息</h3><van-form @submit="提交表单"><van-field v-model="year[0]" is-link readonly name="年" label="年份" placeholder="点击选择年份"@click="showYearPicker = true" /><van-popup v-model:show="showYearPicker" position="bottom"><van-picker :columns-field-names='{ value: "text" }' v-model="year" title="农历出生年份" :columns="years"@confirm="showYearPicker=false" /></van-popup><van-field v-model="month[0]" is-link readonly name="月" label="月份" placeholder="点击选择月份"@click="showMonthPicker = true" /><van-popup v-model:show="showMonthPicker" position="bottom"><van-picker :columns-field-names='{ value: "text" }' v-model="month" title="农历出生月份" :columns="months"@confirm="showMonthPicker = false" /></van-popup><van-field v-model="date[0]" is-link readonly name="日" label="日期" placeholder="点击选择日期"@click="showDatePicker = true" /><van-popup v-model:show="showDatePicker" position="bottom"><van-picker :columns-field-names='{ value: "text" }' v-model="date" title="农历出生日期" :columns="dates"@confirm="showDatePicker = false" /></van-popup><van-field v-model="时辰[0]" is-link readonly name="时" label="时辰" placeholder="点击选择时辰"@click="show时辰Picker = true" /><van-popup v-model:show="show时辰Picker" position="bottom"><van-picker :columns-field-names='{ value: "text" }' v-model="时辰" title="农历出生时辰" :columns="时辰们"@confirm="show时辰Picker = false" /></van-popup><br /><van-button round block type="primary" native-type="submit">提交</van-button></van-form><br /><p ><strong>{{info}}</strong></p></div>
</template>
<style scoped>
.p-home {padding: 1rem;h3 {text-align: left;line-height: 3rem;/* font-weight: bold; */}
}
</style>
这篇关于vue3+vant4中表单内嵌picker的默认值设置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!