<van-calendar type="range" :allow-same-day="true" :default-date="defaultDate" />
<script setup lang="ts">
import { ref } from 'vue';
const defaultDate = ref([new Date(), new Date()]);
</script>
然后会发现日历默认选中的并不是今天-今天,而是今天-明天。后面经过坚持不懈的搜索及尝试,找到了解决方案:
<van-calendar type="range" :allow-same-day="true" :default-date="defaultDate" />
<script setup lang="ts">
import { ref, onMounted } from 'vue';
const defaultDate = ref([]);
onMounted(() => {
defaultDate.value = [new Date(), new Date()];
});
</script>
原因是Calendar组件的allow-same-day参数默认值是false,即不允许日期范围的起止时间为同一天,所以一开始初始化赋予起止为同一天是无效的。需要等页面元素渲染完成之后(onMounted)再进行赋值,这时候日历的回显的默认值就是今天-今天啦!完美撒花~