伙伴匹配系统(Vant4)踩坑 前言 鱼皮直播做的伙伴匹配系统前端使用的是Vant3,本人跟着视频做的时候,听说Vant4 相比于Vant3进行了优化,就作死的使用了Vant4,就出现以下坑点
Toast Vant3 中 弹窗类的方法是
1 2 3 4 5 6 impor {Toast} form "vant" ... Toast.success(); Toast.fail(); ...
到 Vant4,实现上述功能,要改成
1 2 3 4 5 6 import {showSuccessToast, showFailToast} form "vant" ... showSuccessToast(); showFailToast(); ...
DateTimePicker 关于这个组件,Vant4 的 官方文档 说 picker 直接重构了!!!!!!!!
对于一个后端选手来说实在是太痛苦了o(╥﹏╥)o
在Vant3中,第十一期伙伴匹配系统的前端使用的DateTimePicker是这样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 <template> <div id="teamAddPage"> ... <van-field is-link readonly name="datetimePicker" label="过期时间" :placeholder="addTeamData.expireTime ?? '点击选择过期时间'" @click="showPicker = true" /> <van-popup v-model:show="showPicker" position="bottom"> <van-datetime-picker v-model="addTeamData.expireTime" @confirm="showPicker = false" type="datetime" title="请选择过期时间" :min-date="minDate" /> </van-popup> ... </div> </template> <script setup lang="ts"> import {useRouter} from "vue-router"; import {ref} from "vue"; import myAxios from "../plugins/myAxios"; import {Toast} from "vant"; const router = useRouter(); // 展示日期选择器 const showPicker = ref(false); const minDate = new Date(); const initFormData = { "name": "", "description": "", "expireTime": null, "maxNum": 3, "password": "", "status": 0, } // 需要用户填写的表单数据 const addTeamData = ref({...initFormData}) // 提交 const onSubmit = async () => { const postData = { ...addTeamData.value, status: Number(addTeamData.value.status) } // todo 前端参数校验 const res = await myAxios.post("/team/add", postData); if (res?.code === 0 && res.data){ Toast.success('添加成功'); router.push({ path: '/team', replace: true, }); } else { Toast.success('添加失败'); } } </script>
到了Vant4,DateTimePicker 被拆分成 DatePicker 和 TimePicker,如果要同时使用两个或多个 Picker,要使用 PickerGroup
上面的代码就要修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 <template> <div id="teamAddPage"> ... <van-field is-link readonly clickable name="date-picker" label="过期时间" :value="addTeamData.expireTime" v-model="addTeamData.expireTime" :placeholder="'点击选择过期时间'" @click="showPicker = true" /> <van-popup v-model:show="showPicker" position="bottom"> <van-picker-group title="设定过期日期" :tabs="['选择日期', '选择时间']" @confirm="onConfirm" @cancel="showPicker = false" > <van-date-picker v-model="currentDate" :min-date="minDate" /> <van-time-picker v-model="currentTime" :columns-type="columnsType" /> </van-picker-group> </van-popup> ... </div> </template> <script setup lang="ts"> import {useRoute, useRouter} from "vue-router"; import {ref} from "vue"; import myAxios from "../plugins/myAxios"; import {showSuccessToast, showFailToast} from "vant"; const router = useRouter(); const route = useRoute(); // 展示日期选择器 const showPicker = ref(false); const minDate = new Date(); const initFormData = { "name": "", "description": "", "expireTime": "", //这里不能用null(我也不明白为啥) "maxNum": 3, "password": "", "status": 0, } const currentDate = ref(['2022', '06', '01']); //定义一个初始时间(年月日) const currentTime = ref(['12', '00', '00']); //定义一个初始时间(时分秒) const columnsType = ['hour', 'minute', 'second']; const onConfirm = () => { //组合过期时间,'T'是满足后端序列化配的 addTeamData.value.expireTime = currentDate.value.join('-') + 'T' + currentTime.value.join(':'); showPicker.value = false; //有了这行才会使picker点击“确认”后自动关闭 //测试picker返回的值 // showToast( // addTeamData.value.expireTime // ); // console.log("addTeamData.value.expireTime: " + addTeamData.value.expireTime); }; // 需要用户填写的表单数据 const addTeamData = ref({...initFormData}) // 提交 const onSubmit = async () => { const postData = { ...addTeamData.value, status: Number(addTeamData.value.status) } // todo 前端参数校验 const res = await myAxios.post("/team/add", postData); if (res?.code === 0 && res.data) { showSuccessToast('添加成功'); router.push({ path: '/team', replace: true, }); } else { showFailToast('添加失败'); } } </script>