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)
相关推荐
mCell5 小时前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip5 小时前
Node.js 子进程:child_process
前端·javascript
excel8 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel9 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼10 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping10 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙11 小时前
[译] Composition in CSS
前端·css
白水清风11 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix12 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户221520442780012 小时前
new、原型和原型链浅析
前端·javascript