uni-app
uni-app copied to clipboard
picker-view在设置默认值的时候失效
发行方式
小程序
具体平台
微信小程序
开发环境
Windows
项目创建方式
CLI命令行
依赖版本
"@dcloudio/uni-app": "3.0.0-4020920240930001", "@dcloudio/uni-app-harmony": "3.0.0-4020920240930001", "@dcloudio/uni-app-plus": "3.0.0-4020920240930001", "@dcloudio/uni-components": "3.0.0-4020920240930001", "@dcloudio/uni-h5": "3.0.0-4020920240930001", "@dcloudio/uni-mp-alipay": "3.0.0-4020920240930001", "@dcloudio/uni-mp-baidu": "3.0.0-4020920240930001", "@dcloudio/uni-mp-jd": "3.0.0-4020920240930001", "@dcloudio/uni-mp-kuaishou": "3.0.0-4020920240930001", "@dcloudio/uni-mp-lark": "3.0.0-4020920240930001", "@dcloudio/uni-mp-qq": "3.0.0-4020920240930001", "@dcloudio/uni-mp-toutiao": "3.0.0-4020920240930001", "@dcloudio/uni-mp-weixin": "3.0.0-4020920240930001", "@dcloudio/uni-mp-xhs": "3.0.0-4020920240930001", "@dcloudio/uni-quickapp-webview": "3.0.0-4020920240930001",
问题描述
<template>
<picker-view
class="custom-picker-view"
:value="pickerValue"
@change="onStartPickerChange"
>
<picker-view-column>
<view v-for="item in yearRange" :key="item" class="picker-item">{{ item }}年</view>
</picker-view-column>
<picker-view-column>
<view v-for="item in monthRange" :key="item" class="picker-item">{{ item }}月</view>
</picker-view-column>
<picker-view-column>
<view v-for="(item, idx) in dayRange" :key="item" class="picker-item">
{{ item }}日
</view>
</picker-view-column>
</picker-view>
</template>
<script lang="ts" setup>
const now = new Date()
const thisYear = now.getFullYear()
const yearRange = [thisYear - 1, thisYear, thisYear + 1]
const monthRange = Array.from({ length: 12 }, (_, i) => i + 1)
const getDaysInMonth = (year: number, month: number) => {
return new Date(year, month, 0).getDate()
}
const dayRange = ref<number[]>([])
const pickerValue = ref<[number, number, number]>([1, now.getMonth(), now.getDate() - 1]) // 默认选中今年、当月、当天
const updateDayRange = () => {
const year = yearRange[pickerValue.value[0]]
const month = monthRange[pickerValue.value[1]]
const days = getDaysInMonth(year, month)
dayRange.value = Array.from({ length: days }, (_, i) => i + 1)
// 防止天数越界
if (pickerValue.value[2] >= dayRange.value.length) {
pickerValue.value[2] = dayRange.value.length - 1
}
}
onMounted(() => {
updateDayRange()
const month = now.getMonth()
// Tip: 暂时无法理解为什么-1就无法定位到默认值
// const day = now.getDate() - 1
const day = now.getDate()
pickerValue.value = [1, month, day]
})
</script>
重现步骤
// Tip: 暂时无法理解为什么-1就无法定位到默认值 // const day = now.getDate() - 1 这两行代码出现的问题。无法理解为什么 - 1就会导致定位到当日出现问题。用下面的 const day = now.getDate()就正常
期望行为
正常定位到当日
实际行为
No response
截图或录屏
实际结果
(期望结果)