dcloudio / dcloudio/uni-app

picker-view在设置默认值的时候失效

Open
#5,434 1 comment 0 reactions 0 assignees View on GitHub
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_

### 截图或录屏

实际结果

![Image](https://github.com/user-attachments/assets/857945cc-c6f8-4dbd-9d43-2a9e87a80db1)

(期望结果)![Image](https://github.com/user-attachments/assets/f08314d6-6d04-4972-9dc2-f20e09c3efe0)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.