Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 4 typescript解析响应对象中的枚举接口属性_Angular_Typescript_Ionic2 - Fatal编程技术网

Angular 4 typescript解析响应对象中的枚举接口属性

Angular 4 typescript解析响应对象中的枚举接口属性,angular,typescript,ionic2,Angular,Typescript,Ionic2,我有一个来自API的响应,它返回一个枚举值。API返回的值在请求中表示为字符串。 此值是typescript接口的enum属性 问题: 当接收到响应时,TS接口将该值存储为字符串(可能就是这个问题),因此我不能将其直接用作enum obj型号: export interface Condo { id:number title:string latitude:number longitude:number city:string country:string dis

我有一个来自API的响应,它返回一个枚举值。API返回的值在请求中表示为字符串。 此值是typescript接口的
enum
属性

问题: 当接收到响应时,TS接口将该值存储为字符串(可能就是这个问题),因此我不能将其直接用作
enum

obj型号:

export interface Condo {

  id:number
  title:string
  latitude:number
  longitude:number

  city:string
  country:string
  district:string
  address:string
  locationType: LocationType
}

export enum LocationType {
  CONDO,
  MALL,
  STATION
}
getCondoAllByCountry(country_code){
    return this.http.get(this.config.apiEndpoint +this.subApiUrl+'/all')
      .map(res => <Condo[]>res.json())
      .catch((err:Response) => {
        return Observable.throw(err.json());
      });
    }
    this.condoService.getCondoAllByCountry(this.userData.country_code).subscribe(data=>{
          someFunc(data)
        })

............
    someFunc(condo_list: Condo[]){
    //here is need to know the `locationType` for each object
      console.log(typeof condo_list[i].locationType);
      console.log(typeof LocationType.CONDO)
      switch (condo_list[i].locationType){
        case LocationType.CONDO:
          console.log('Case - condo')
          break;
        case LocationType.MALL:
          console.log('Case - mall')
          break;
        case LocationType.STATION:
          console.log('Case - station')
          break;
      }
    }
请求:

export interface Condo {

  id:number
  title:string
  latitude:number
  longitude:number

  city:string
  country:string
  district:string
  address:string
  locationType: LocationType
}

export enum LocationType {
  CONDO,
  MALL,
  STATION
}
getCondoAllByCountry(country_code){
    return this.http.get(this.config.apiEndpoint +this.subApiUrl+'/all')
      .map(res => <Condo[]>res.json())
      .catch((err:Response) => {
        return Observable.throw(err.json());
      });
    }
    this.condoService.getCondoAllByCountry(this.userData.country_code).subscribe(data=>{
          someFunc(data)
        })

............
    someFunc(condo_list: Condo[]){
    //here is need to know the `locationType` for each object
      console.log(typeof condo_list[i].locationType);
      console.log(typeof LocationType.CONDO)
      switch (condo_list[i].locationType){
        case LocationType.CONDO:
          console.log('Case - condo')
          break;
        case LocationType.MALL:
          console.log('Case - mall')
          break;
        case LocationType.STATION:
          console.log('Case - station')
          break;
      }
    }
因此,
开关。。大小写不适用于此属性。在
console.log()
中,我得到:

console.log(公寓列表的类型[i].locationType)-
字符串

console.log(type of LocationType.CONDO)
-
number

因此,这意味着存在解析问题和
condo_列表[i]。locationType
不是
enum
(考虑到它应该为enum显示
number


如何修复它?

如果您使用的是typescript 2.4或更高版本,您可以按如下方式声明字符串枚举:

export enum LocationType {
  CONDO = 'CONDO',
  MALL = 'MALL',
  STATION = 'STATION'
}

// ...

switch (object.locationType) {
    case LocationType.CONDO: // ...
    case LocationType.MALL: // ...
    case LocationType.STATION: // ...
}
在旧版本中,您只能使用基于数字的枚举。在这种情况下,最好使用字符串文字联合类型:

export type LocationType = 'CONDO' | 'MALL' | 'STATION';

// ...

switch (object.locationType) {
    case 'CONDO': // ...
    case 'MALL': // ...
    case 'STATION': // ...
}