vue el-date-picker 日期选择 回显后成功后无法改变的解决办法

在实现一个前端页面默认时间选择时遇到了手动选择日期无法回显但在浏览器vue插件监控属性时却能看到手动选择的值的问题。

html 复制代码
<el-date-picker
  v-else-if="item.type === 'datetPicker'"
  v-model="form[item.prop]"
  :placeholder="item.placeholder"
  :picker-options="item.pickerOptions"
  type="date"
  value-format="yyyy-MM-dd"
  clearable
/>

在钩子函数默认选择昨天

js 复制代码
  created() {
    this.defaultDate()
  }
  defaultDate() {
     let date = new Date();
     const yesterday = date.setDate(date.getDate() - 1);
     this.form.startDate = this.handleTime(yesterday)
     this.form.endDate = this.handleTime(yesterday)
   },
   handleTime(yesterday) {
     let date = new Date(yesterday);
     const year = date.getFullYear();
     // 获取月份,要加 1,格式化为两位数
     const month = String(date.getMonth() + 1).padStart(2, '0');
     // 获取日期,格式化为两位数
     const day = String(date.getDate()).padStart(2, '0');
     const res = year + '-' + month + '-' + day;
     this.$set(this.form,'startDate', res)
     this.$set(this.form,'endDate', res)
     return res;
   }

加上这两行代码就能解决上述问题了。

js 复制代码
this.$set(this.form,'startDate', res)
this.$set(this.form,'endDate', res)
相关推荐
东东5161 小时前
基于ssm的网上房屋中介管理系统vue
前端·javascript·vue.js
harrain2 小时前
什么!vue3.4开始,v-model不能用在prop上
前端·javascript·vue.js
阿蒙Amon7 小时前
TypeScript学习-第7章:泛型(Generic)
javascript·学习·typescript
睡美人的小仙女1278 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
fanruitian8 小时前
uniapp android开发 测试板本与发行版本
前端·javascript·uni-app
rayufo8 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk8 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
摘星编程9 小时前
React Native + OpenHarmony:Timeline垂直时间轴
javascript·react native·react.js
2501_944525549 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
jin12332210 小时前
React Native鸿蒙跨平台完成剧本杀组队详情页面,可以复用桌游、团建、赛事等各类组队详情页开发
javascript·react native·react.js·ecmascript·harmonyos