我有这个if循环,它将isCustomerFunctionSelected
设置为true和false,并将值设置为null,我该如何用TS缩短技术重写它。
this.reset = false;
if(this.functionValue === 'Customer') {
this.isCustomerFunctionSelected = true;
} else if (this.functionValue === 'Dealer') {
this.isCustomerFunctionSelected = false;
this.selectedCustomerValue = null;
} else {
this.reset = true;
}
答案 0 :(得分:1)
不会缩短它,但会使其更易于维护和发展。
const {
func,
} = ([{
value: 'Customer',
func: () => {
this.isCustomerFunctionSelected = true;
},
}, {
value: 'Dealer',
func: () => {
this.isCustomerFunctionSelected = true;
this.selectedCustomerValue = null;
},
}].find(x => x.value === this.functionValue) || {
func: () => {
this.reset = true;
},
});
func();