使用Vant完成DatetimePicker 日期的選擇器操作
效果展示:

代碼展示:
<template>
<div id="date_time_picker">
<van-button plain type="primary" @click="showPopFn()">點(diǎn)擊選擇日期</van-button>
<van-field v-model="timeValue" placeholder="選擇的日期結(jié)果" readonly />
<van-popup v-model="show" position="bottom" :style="{ height: '40%' }">
<van-datetime-picker v-model="currentDate" type="date" @change="changeFn()" @confirm="confirmFn()" @cancel="cancelFn()" />
</van-popup>
</div>
</template>
<script>
export default {
data() {
return {
msg: '',
currentDate: new Date(),
changeDate: new Date(),
show: false, // 用來(lái)顯示彈出層
timeValue: ''
}
},
methods: {
showPopFn() {
this.show = true;
},
showPopup() {
this.show = true;
},
changeFn() { // 值變化是觸發(fā)
this.changeDate = this.currentDate // Tue Sep 08 2020 00:00:00 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
},
confirmFn() { // 確定按鈕
this.timeValue = this.timeFormat(this.currentDate);
this.show = false;
},
cancelFn(){
this.show = true;
},
timeFormat(time) { // 時(shí)間格式化 2019-09-08
let year = time.getFullYear();
let month = time.getMonth() + 1;
let day = time.getDate();
return year + '年' + month + '月' + day + '日'
}
},
mounted() {
this.timeFormat(new Date());
}
}
</script>
<style>
</style>
注意:如果是按需引入的話,記得在main.js里面引入相應(yīng)的文件奧。
// main.js文件里面的部分代碼
import {Button} from 'vant'
import { DatetimePicker } from 'vant';
import { Popup } from 'vant';
import { Field } from 'vant';
Vue.use(Button)
Vue.use(DatetimePicker)
Vue.use(Popup)
Vue.use(Field);
---------完。
補(bǔ)充知識(shí):小程序使用vant組件庫(kù)里的DatetimePicker 時(shí)間選擇的使用,以及如何使用里面的函數(shù)
小程序使用vant組件庫(kù)里的DatetimePicker 時(shí)間選擇的使用(困擾我最大的坑,聽(tīng)我慢慢細(xì)說(shuō))?
由于我想要在小程序里加入vant庫(kù)的時(shí)間選擇器,看了一下vant的官方文檔后,十分覺(jué)得里面說(shuō)的太過(guò)簡(jiǎn)潔,讓我看的很迷:
第一點(diǎn)看不懂的是明明例子都有寫(xiě)出來(lái)了,代碼還沒(méi)有貼出來(lái),只貼出了基本的例子的代碼:

下面這個(gè)代碼:

效果也不是上圖的效果;
一些參數(shù)的解釋讓我也饒了一大圈,才成功通過(guò)改這些參數(shù),成功弄成這個(gè)樣式出來(lái)
會(huì)讓人誤會(huì)的說(shuō)明:

后面我把這個(gè)type改成year-month-time就可以了,具體代碼如下:
<van-datetime-picker
type="year-month-time"
value="{{ currentDate }}"
min-date="{{ minDate }}"
bind:input="onInput"
bind:confirm="onConfirm"
bind:change="onChange"
/>
data:{
minHour: 0,
maxHour: 24,
minDate: new Date().getTime(),
currentDate: new Date().getTime(),
}
onInput(event) {
this.setData({
currentDate: event.detail,
});
},
就可以呈現(xiàn)出上面那種效果了。
第二點(diǎn)看不懂的是:如何使用change里的函數(shù)(文檔里面也沒(méi)有例子,也沒(méi)有說(shuō)明,而網(wǎng)上大部分都是網(wǎng)頁(yè)來(lái)使用,半點(diǎn)沒(méi)有涉及到小程序的,就算有涉及到,也是亂說(shuō)的,或者是如何使用vant而已):

解決方案:使用里面的event.detail來(lái)調(diào)用getValues(),而不能使用event.getValues().
onChange(event){
this.setData({
reserveCopyTime:event.detail.getValues(),
})
},
以上這篇使用Vant完成DatetimePicker 日期的選擇器操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 如何解決element-ui中select下拉框popper超出彈框問(wèn)題
- element-ui中select下拉框加載大數(shù)據(jù)渲染優(yōu)化方式
- 去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn)
- 基于element-ui中el-select下拉框選項(xiàng)過(guò)多的優(yōu)化方案
- 關(guān)于element-ui?select?下拉框位置錯(cuò)亂問(wèn)題解決
- element-ui+vue-treeselect下拉框的校驗(yàn)過(guò)程
- 解決element-ui的下拉框有值卻無(wú)法選中的情況
- Vue + Element-ui的下拉框el-select獲取額外參數(shù)詳解
- 在element-ui的select下拉框加上滾動(dòng)加載
- 如何修改element-ui日期下拉框datetimePicker的背景色樣式
相關(guān)文章
vue給數(shù)組中對(duì)象排序 sort函數(shù)的用法
這篇文章主要介紹了vue給數(shù)組中對(duì)象排序 sort函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式
這篇文章主要介紹了vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
nuxt.js 在middleware(中間件)中實(shí)現(xiàn)路由鑒權(quán)操作
這篇文章主要介紹了nuxt.js 在middleware(中間件)中實(shí)現(xiàn)路由鑒權(quán)操作,具有很好的參考價(jià)值,希望大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Vue動(dòng)態(tài)路由緩存不相互影響的解決辦法
這篇文章主要介紹了Vue動(dòng)態(tài)路由緩存不相互影響的解決辦法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
Vue實(shí)現(xiàn)關(guān)聯(lián)頁(yè)面多級(jí)跳轉(zhuǎn)(頁(yè)面下鉆)功能的完整實(shí)例
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)關(guān)聯(lián)頁(yè)面多級(jí)跳轉(zhuǎn)(頁(yè)面下鉆)功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

