el-descriptions-item label=消费金额 v-if=formcardType == 0 && formconsumptionType == 1 labelStyle=background #F5F7FA; formcostAmount 元el-descriptions-item el-descriptions-item label=消费次数 v-if=form
可以考虑将重复的代码进行提取,使用计算属性来动态计算需要显示的label和value。
首先,创建一个计算属性来计算需要显示的label和value:
computed: {
label() {
if (this.form.cardType == 0 && this.form.consumptionType == 1) {
return "消费金额";
} else if (this.form.cardType == 1 && this.form.consumptionType == 1) {
return "消费次数";
} else if (this.form.cardType == 0 && this.form.consumptionType == 0) {
return "充值金额";
} else if (this.form.cardType == 1 && this.form.consumptionType == 0) {
return "充值次数";
} else {
return "";
}
},
value() {
if (this.form.cardType == 0 && this.form.consumptionType == 1) {
return this.form.costAmount + " 元";
} else if (this.form.cardType == 1 && this.form.consumptionType == 1) {
return this.form.costTimes + " 次";
} else if (this.form.cardType == 0 && this.form.consumptionType == 0) {
return this.form.costAmount + " 元";
} else if (this.form.cardType == 1 && this.form.consumptionType == 0) {
return this.form.costTimes + " 次";
} else {
return "";
}
}
}
然后,在模板中使用计算属性来显示label和value:
<el-descriptions-item v-if="label" :label="label" labelStyle="background: #F5F7FA;">{{ value }}</el-descriptions-item>
通过使用计算属性和简化代码逻辑,可以优化重复代码的问题
原文地址: http://www.cveoy.top/t/topic/iC3S 著作权归作者所有。请勿转载和采集!