优化前
if (that.idData) {
uni.showToast({
title: '功能完善中',
icon: 'error',
duration: 1000
});
return
}
if (that.addressMap=='') {
uni.showToast({
title: '请选择地址',
icon: 'none',
duration: 2000
})
} else if(that.detailsAddress==''){
uni.showToast({
title: '请输入门牌号',
icon: 'none',
duration: 2000
})
}else if(that.consignee==""){
uni.showToast({
title: '请输入联系人',
icon: 'none',
duration: 2000
})
}else if(that.consigneeTel==''){
uni.showToast({
title: '请输入手机号',
icon: 'none',
duration: 2000
})
}else{
console.log("判断后的数据");
}
优化后
然后 我们通过find方法 去找到数组的每一个元素的第一个方法调用,可以看到我这里写了五个不同的条件,if也只需要写一次
showToast(title, icon) {
uni.showToast({
title,
icon,
duration: 1000
});
},
const rule=[
[()=>that.idData,()=>that.showToast("功能完善中",'error')],
[()=>that.addressMap=='',()=>that.showToast("请选择地址",'error')],
[()=>that.detailsAddress=='',()=>that.showToast("请输入门牌号",'error')],
[()=>that.consignee=="",()=>that.showToast("请输入联系人",'error')],
[()=>that.consigneeTel=="",()=>that.showToast("请输入手机号",'error')],
]
const target = rule.find((m) => m[0]());
if (target) {
return target[1]();
}
console.log("判断后的数据");