js if else循环如何用ts缩短技术编写

时间:2019-10-09 08:50:10

标签: typescript angular8

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

1 个答案:

答案 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();
相关问题