Vue Element UI 之 date-picker 禁用時間和報錯:TypeError: Cannot read property 'getHours' of undefined

在這裏插入圖片描述
一、場景:使用 date-picker 日期選擇器,並禁用日期

二、報錯TypeError: Cannot read property 'getHours' of undefined

三、原因和解決方法:綁定數據的格式需要是字符串類型

四、詳情:由於粗心,誤了方向,以爲是控件的屬性寫的不對,百度了一下,才知道是綁定的數據格式寫錯了

date: []

修改回來,寫成字符串就好了

date: ''

五、完整代碼

<template>
	<el-date-picker v-model="addForm.selectDates" :picker-options="pickerOptions" type="date" placeholder="選擇日期"></el-date-picker>
</template>

<script>
	data() {
		return{
			pickerOptions: {
				// 禁用時間(返回值爲boolean)
				disabledDate(time) {
					// return time.getTime() < Date.now();// 禁用當前時間之前的時間
					return time.getTime() > Date.now();// 禁用當前時間之後的時間
				}
			},
			addForm: {
				selectDates: ''
			}
		}
	}
	
</script>					

在這裏插入圖片描述
六、效果

左圖:禁用當前日期之後的日期;右圖:禁用當前日期之前的日期(包括當前日期)

部分參考自:https://blog.csdn.net/qq_38194393/article/details/90690611?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章