需求:
计划年度(plan字段):当前年份
统计区间(始)(statisticsStart字段):监听计划年度,为计划年度的1月1日
统计区间(末)(statisticsEnd字段):监听计划年度,为计划年度的12月31日
如下图所示:
1.初始化计划年度的值
注:这里date+‘ ’,是因为elementui的日期选择器接收的是字符串,而new Date().getFullYear()是Date数据类型,如果不转换成字符串,会报如下错:
mounted() {
if (this.id == "new") {
this.isEdit = false;
var date = new Date().getFullYear()
date = date + ''
this.$set(this.formInline,'plan',date)
} else {
this.getList();
}
},
二、监听plan字段
注:这里必须使用this. s e t 来 赋 值 , 不 能 直 接 赋 值 , 否 则 虽 然 可 以 显 示 时 间 , 但 日 期 选 择 器 的 时 间 不 能 选 择 , 换 句 话 说 , 没 有 监 听 到 时 间 的 变 化 , 所 以 一 定 要 用 t h i s . set来赋值,不能直接赋值,否则虽然可以显示时间,但日期选择器的时间不能选择,换句话说,没有监听到时间的变化,所以一定要用this. set来赋值,不能直接赋值,否则虽然可以显示时间,但日期选择器的时间不能选择,换句话说,没有监听到时间的变化,所以一定要用this.set来添加属性并赋值
watch: {
'formInline.plan': {
handler() {
console.log(this.formInline.plan,'this.formInline.plan---')
this.$set(this.formInline,'statisticsStart', this.formInline.plan +'-01-01 20:46:17')
this.$set(this.formInline,'statisticsEnd', this.formInline.plan +'-12-31 20:46:17')
console.log(this.formInline)
},
}
},