Ich habe eine Antwort von API, die einen Enum-Wert zurückgibt. Der von der API zurückgegebene Wert wird als Zeichenfolge in der Anforderung dargestellt. Dieser Wert ist ein enum
Attribut der Typoskript-Schnittstelle.Angular 4 typescript Parsing enum Interface-Attribut in Antwortobjekt
Problem: beim Empfang eine Antwort, TS-Schnittstelle Speichers, der als String-Wert (wahrscheinlich das ist das Problem), so kann ich es nicht direkt als enum
verwenden.
obj Modell:
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
}
Anfrage:
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());
});
}
Anwendungsbeispiel:
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;
}
}
Also, die 012.funktioniert nicht für dieses Attribut. in console.log()
ich erhalte:
console.log(typeof condo_list[i].locationType);
-string
console.log(typeof LocationType.CONDO)
-number
Also, das bedeutet, dass ein Parsing prob dort waren und condo_list[i].locationType
ist kein enum
(wenn man bedenkt es number
für Enum zeigen sollte) ?
Wie soll ich es beheben?
10 und was ist, wenn ich 'typescript <2.4' (2.3.4) verwende? – user1935987
Lesen Sie die zweite Hälfte meiner Antwort? –
ops. manchmal gehen die Dinge einfach vor den Augen vorbei: D – user1935987