如果 else 循环可以用 ts 短端技术编写,JS怎么能



我有这个 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;
}

不会缩短它,但会更容易维护和发展。

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();

最新更新