比较Enum Angular 4

时间:2017-07-20 09:38:20

标签: angular

oportunidad.model.ts:

const enum TipoSiNo {
SI, NO
}


export class Oportunidad {
constructor(
  public resultadoValidacion?: TipoSiNo,

我需要使用枚举的一个可能值来购买我的对象的值:

oportunidad-edit.component.ts:

添加:

const enum TipoSiNo {
SI, NO
}

this.oportunidad.resultadoValidacion === TipoSiNo.NO

给我以下错误

ERROR in [at-loader] ./src/main/webapp/app/entities/oportunidad/oportunidad-edit.component.ts:337:75 
TS2365: Operator '===' cannot be applied to types 'TipoSiNo.NO' and 'TipoSiNo.NO'.

2 个答案:

答案 0 :(得分:1)

试试这个:

this.oportunidad.resultadoValidacion as TipoSiNo === TipoSiNo.NO as TipoSiNo

答案 1 :(得分:0)

我可以看到你在两个不同的地方创建相同的枚举:TipoSiNo,因此两者都是两种不同的类型,因此比较不起作用。

我来到这里是为了找到为什么我无法比较来自同一个枚举的两个枚举值,但无法在任何地方得到任何答案。例如:

export enum Stage{
NotStarted, QuarterOf, Half, QuarterTo, Full
}

export class Work{
id: number,
title: string,
description: string,
status: Stage
}

// check if work is not started
if(this.currentWork===Stage.NotStarted){ // <- this is never true
   // do something
}

所以,我找到了一个解决方法:

// check if work is not started
if(this.currentWork.toString===Stage[Stage.NotStarted]){ // <- this works
   // do something
}

希望它有助于某人