用ts枚举优化多个if语句

原来的代码:

function confirm(val, row) {
  //查看镜像详情
  if (val == 'viewDetail') {
    console.log(1111111,val)
    generalMirrorDialogRef.value.visible = true
    
  }
  //编辑
  if (val == 'edit') {
    console.log(2222222,val)
    generalMirrorDialogRef.value.visible = true
    generalMirrorDialogRef.value.title = '编辑镜像'
  }
  //删除
  if (val == 'delete') {
    deleteMirrorDialogRef.value.visible = true
    deleteMirrorDialogRef.value.deleteMessage=row[0].mirrorName
  }

优化以后的:

//枚举
enum ConfirmationAction {
  VIEW_DETAIL = 'viewDetail',
  EDIT = 'edit',
  DELETE = 'delete',
}

function confirm(val: ConfirmationAction, row: any) {
  switch (val) {
    //查看镜像详情
    case ConfirmationAction.VIEW_DETAIL:
      generalMirrorDialogRef.value.visible = true;
      break;
    case ConfirmationAction.EDIT:
      //编辑
      generalMirrorDialogRef.value.visible = true;
      generalMirrorDialogRef.value.title = '编辑镜像';
      break;
    case ConfirmationAction.DELETE:
      //删除
      deleteMirrorDialogRef.value.visible = true;
      deleteMirrorDialogRef.value.deleteMessage = row[0].mirrorName;
      break;
    default:
      break;
  }
}

你可能感兴趣的:(前端,javascript)