1,注册DataTimePicker组件
import { DatetimePicker } from 'vant'
Vue.use(DatetimePicker)
2,在UserEdit.vue页面使用, 定义相关变量
<!-- 修改时间 -->
<van-popup v-model="isShowBirth" position="bottom" style="height: 50%" round>
<!-- 日期选择控件 -->
<van-datetime-picker
v-model="currentDate"
type="date"
title="选择出生日期"
:min-date="minDate"
:max-date="maxDate"
:show-toolbar="true"
/>
</van-popup>
<script>
export default {
data () {
return {
isShowBirth: false,
minDate: new Date(1900, 0, 1),
maxDate: new Date(),
currentDate: new Date()
}
},
}
</script>
3,点击单元格, 弹出时间选择组件
<van-cell title="生日" is-link :value="profile.birthday" @click="isShowBirth = true"/>
4,绑定事件, 实现功能
<!-- 修改时间 -->
<van-popup
v-model="isShowBirth"
position="bottom"
style="height: 50%"
round>
<!-- 日期选择控件 -->
<van-datetime-picker
v-model="currentDate"
type="date"
title="选择出生日期"
:min-date="minDate"
:max-date="maxDate"
:show-toolbar="true"
@cancel="isShowBirth = false"
@confirm="confirmFn"/>
</van-popup>
<script>
import moment from 'moment'
export default {
methods: {
showBirthFn () {
this.isShowBirth = true
this.currentDate = new Date(this.profile.birthday)
},
async confirmFn () {
console.log(this.currentDate)
const dateStr = moment(this.currentDate).format('YYYY-MM-DD')
await updateProfileAPI({
birthday: dateStr
})
this.profile.birthday = dateStr
this.isShowBirth = false
}
}
}
</script>
|