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

编程入门 行业动态 更新时间:2024-10-21 19:41:24
本文介绍了Angular 4 typescript解析响应对象中的枚举接口属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a response from API which is returning a enum value. value returned from API is represented as a string in request. This value is a enum attribute of typescript interface.

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

Problem: when receiving a response, TS interface store that value as a string (probably thats the issue) so i can't use it directly as enum.

对象模型:

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; } }

因此,switch..case不适用于此属性.在 console.log() 我得到:

So, the switch.. caseis not working for this attribute. in console.log() i'm getting:

console.log(typeof condo_list[i].locationType); - string

console.log(typeof LocationType.CONDO) - number

所以,这意味着存在解析问题,并且 condo_list[i].locationType 不是 enum(考虑到它应该显示 number 对于枚举)?

So, that means that there were a parsing prob and condo_list[i].locationType is not a enum (considering it should show number for enum)?

我应该如何解决它?

推荐答案

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

If you're using typescript 2.4 or above, you can declare string enums as follows:

export enum LocationType { CONDO = 'CONDO', MALL = 'MALL', STATION = 'STATION' } // ... switch (object.locationType) { case LocationType.CONDO: // ... case LocationType.MALL: // ... case LocationType.STATION: // ... }

在旧版本中,您只能使用基于数字的枚举.在这种情况下,您最好使用字符串文字联合类型:

In older versions, you're restricted to using number-based enums. In that case, you're probably better off using a string literal union type:

export type LocationType = 'CONDO' | 'MALL' | 'STATION'; // ... switch (object.locationType) { case 'CONDO': // ... case 'MALL': // ... case 'STATION': // ... }

更多推荐

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

本文发布于:2023-06-12 05:18:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/652054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:象中   属性   接口   Angular   typescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!