写判断不要再全是 if else 了,map

给小伙伴 cr 代码 ~

changeTabs({ key }) {
  this.tabsType = key;
  this.$refs.comTable.cancelCheckList();
  if (key === 'approved') {
    this.approvalType = 20;
    this.store.type.approvalType = 20;
  } else if (key === 'pendingApproval') {
    this.approvalType = 10;
    this.store.type.approvalType = 10;
  } else if (key === 'initiatedApproval') {
    this.approvalType = 30;
    this.store.type.approvalType = 30;
  } else {
    this.approvalType = 40;
    this.store.type.approvalType = 40;
  }
}

看的出来这个方法主要是做三件事
tabsType = key
根据 key 给 approvalType和this.store.type.approvalType 赋予一个状态码
this.$refs.comTable.cancelCheckList();

CR 后

changeTabs({ key }) {
  const typeMap = { 'approved': 20, 'pendingApproval': 10, 'initiatedApproval': 30, 'transcribe': 40 };
  this.tabsType = key;
  this.approvalType = typeMap[key];
  this.$refs.comTable.cancelCheckList();
}

你可能感兴趣的:(简单的,CR,列子,前端,javascript)