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)
相关推荐
倾颜3 小时前
从 textarea 到 AI 输入框:用 Tiptap 实现 / 命令、@ 引用和结构化请求
前端·langchain·next.js
kyriewen4 小时前
程序员连夜带团队跑路,省了23万:这AI太贵,真的用不起了
前端·javascript·openai
kyriewen5 小时前
你写的代码没有测试,就像出门不锁门——Jest + Testing Library 从入门到不慌
前端·单元测试·jest
yuzhiboyouye6 小时前
web前端英语面试
前端·面试·状态模式
canonical_entropy7 小时前
下一代低代码渲染框架 nop-chaos-flux 的设计原则
前端·低代码·前端框架
东方小月7 小时前
5分钟搞懂Harness Engineering(驾驭工程):从提示词到AI Agent的进化之路
前端·后端·架构
我叫黑大帅7 小时前
为什么需要 @types/react?解决“无法找到模块 react 的声明文件”报错
前端·javascript·面试
之歆7 小时前
DAY_21JavaScript 深度解析:数组(Array)与函数(Function)(一)
前端·javascript
XinZong8 小时前
【AI社交】基于OpenClaw自研轻量化AI社交平台实战
前端
Le_ee8 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php