在computed中修改data中的数据时候,eslint会提示一个警告,eslint-disable-next-line vue/no-side-effects-in-computed-properties
,原因是因为在computed中不允许直接修改data中的数据,可以将需要修改的数据封装成一个method方法,在computed中调用即可。
原代码为:
computed: {
startAndEndTime() {
if (this.creditsRuleForm.startAndEndTime) {
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
this.creditsRuleForm.startTime = dayjs(this.creditsRuleForm.startAndEndTime[0]).format('YYYY-MM-DD HH:mm:ss')
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
this.creditsRuleForm.endTime = dayjs(this.creditsRuleForm.startAndEndTime[1]).format('YYYY-MM-DD HH:mm:ss')
return this.creditsRuleForm.startTime + ' - ' + this.creditsRuleForm.endTime
} else {
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
this.creditsRuleForm.startTime = ''
// eslint-disable-next-line vue/no-side-effects-in-computed-properties
this.creditsRuleForm.endTime = ''
return ''
}
}
},
修改后:
computed: {
startAndEndTime() {
return this.computeStartAndEndTime()
}
},
methods: {
/**
* 动态返回展示的时间
*/
computeStartAndEndTime() {
if (this.activityRuleForm.startAndEndTime) {
this.activityRuleForm.startTime = dayjs(this.activityRuleForm.startAndEndTime[0]).format('YYYY-MM-DD HH:mm:ss')
this.activityRuleForm.endTime = dayjs(this.activityRuleForm.startAndEndTime[1]).format('YYYY-MM-DD HH:mm:ss')
return this.activityRuleForm.startTime + ' - ' + this.activityRuleForm.endTime
} else {
this.activityRuleForm.startTime = ''
this.activityRuleForm.endTime = ''
return ''
}
}
}