寫個簡單的日期選擇器,還沒搞樣式,所以有點丑
大概長這樣吧
首先是這個picker選擇器,mode選擇日期,end是寫一個范圍前日期,:end就是這個日期是動態變化的,還有change函數
<template><view><view>發生時間</view><picker mode="date" :end="endDate"@change="bindDateChange"><view>{{date}}</view></picker></view>
</template>
然后是腳本,就默認顯示當前日期,設計最后范圍日期為今日日期,再有一個事件函數
<script>export default {data() {return {date: this.getDate()}},computed:{endDate(){return this.getDate();}},methods: {getDate() {const date = new Date();let year = date.getFullYear();let month = date.getMonth() + 1;let day = date.getDate();month = month > 9 ? month : '0' + month;day = day > 9 ? day : '0' + day;return `${year}-${month}-${day}`;},bindDateChange: function(e) {this.date = e.detail.value;}}}
</script>