repair/details.vue组件方法优化
getMaxNumber
#使用es6扩展运算符即可求出数组最大值
getMaxNumber(arr) {
return Math.max(...arr)
}
repair/insert.vue
#简单的if else判断都可以用三元表达式处理优化
data: function () {
this.data.show = this.data.show === true ? true :false
}
#selectModelName方法优化,童鞋们平时自己写也要注意,迭代遍历的方法中(for...of/map/forEach/for...in都有其使用场景,更是当数据量级到一定程度上的话,当前方法中从优化层面上来说。并不是要映射新数组,所以使用for...of即可,也更加安全)
selectModeName (val) {
for (let brand of this.brandModel) {
if (brand.dataName === val) {
this.bugEntityList.modelCode = brand.dataCode;
if (val === "其他") {
this.isShowModelName = true;
}else {
this.isShowModelName = false;
this.bugEntityList.modelName = "";
}
}
}
}
*if else优化方案(代码存在很多的多级if else,使用职责链模式即可基本完成重构,后期如果增加业务功能,只需修改职责链数组长度即可,如若嵌套层次复杂,扁平化数组,故此不在这里一一赘述)
#在代码实现上,我们可以通过一个职责链数组来定义与 else if 完全等效的规则:
const rules = [
{
match: function (a, b, c) { /* ... */ },
action: function (a, b, c) { /* ... */ }
},
{
match: function (a, b, c) { /* ... */ },
action: function (a, b, c) { /* ... */ }
},
{
match: function (a, b, c) { /* ... */ },
action: function (a, b, c) { /* ... */ }
}
// ...
]
function demo (a, b, c) {
for (let i = 0; i < rules.length; i++) {
if (rules[i].match(a, b, c)) {
return rules[i].action(a, b, c)
}
}
}