记录一下 vue2 + elementui 就是表格,默认是当天时间的数据,时间是必填的,也是找了很多方法,原生的 default-value ,失败了,
其实思路很简单,就是需要先拿到 当天日期并格式化成自己想要的数据,关键是需要和日期选择框进行绑定。
<el-date-picker
v-model="queryData.playDay"
type="date"
size="small"
value-format="yyyyMMdd" // 后端要求的格式
>
</el-date-picker>
data() {
return {
queryData: {
playDay: ''
}
}
},
created() {
this.getNowTime() // 需要先将当天日期赋值给 el-date-picker
this.getList() // 请求表格数据 这两个不可以调换顺序,前端请求数据时,加了个验证,日期不能为空,所以页面一加载的时候会报错,当前日期为空
},
methods: {
getNowTime(){
const yy = new Date().getFullYear()
const MM = (new Date().getMonth() + 1) < 10 ? '0' + (new
Date().getMonth() + 1) : (new Date().getMonth() + 1)
const dd = new Date().getDate() < 10 ? '0' + newDate().getDate() : new
Date().getDate()
this.queryData.playDay = yy + MM + dd // 这块是后端要求的日期格式:20230921
},
}