vue3+ant design vue实现日期选择器默认显示当前年,并限制用户只能选择当前年及之前~
1、思路:之前想拿当前年直接做赋值操作,实际上是行不通的,因为组件本身有数据格式限制,会出现报错,然后索性直接获取当前日期(YYYY-MM-DD)赋值给日期组件,这样不管你用的是年,还是月,都可以正确展示。
2、代码:
<a-date-picker
v-model:value="value1"
picker="year"
class="select_width"
:disabledDate="disabledDate"
/>
import dayjs from 'dayjs';
const value1 = ref(dayjs()); //设置当前年
const disabledDate = (current) => {
// 只允许选择当前年及之前的年份
return current && current > dayjs().endOf('year');
};