picker-view在设置默认值的时候失效
- Dominant language
- JavaScript
- Stars
- 41.6k
- Forks
- 3.7k
- Avg merge
- 7h 6m
- Merged PRs (30d)
- 6
Description
### 发行方式
小程序
### 具体平台
微信小程序
### 开发环境
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",
### 问题描述
```ts
{{ item }}年
{{ item }}月
{{ item }}日
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]
})
```
### 重现步骤
// Tip: 暂时无法理解为什么-1就无法定位到默认值
// const day = now.getDate() - 1
这两行代码出现的问题。无法理解为什么 - 1就会导致定位到当日出现问题。用下面的 const day = now.getDate()就正常
### 期望行为
正常定位到当日
### 实际行为
_No response_
### 截图或录屏
实际结果

(期望结果)
Contributor guide
Assessment
This issue has not been assessed yet.