【问题标题】:How to map a response object to a Type interface consisting of a few other Type Interfaces in Angular 7如何将响应对象映射到由Angular 7中的一些其他类型接口组成的类型接口
【发布时间】:2019-08-18 21:06:29
【问题描述】:

我有如下界面:

export interface ObjLookup {
    owner?: IObjOwner;
    contacts?: IOwnerContacts[];
    location?: IOwnerLocation;
}

其中包括以下接口:

export interface IObjOwner {
    lastName?: string,
    firstName?: string;
}

export interface IOwnerContacts {
    name?: string;
    address?: string;
    email?: string;
}

export interface IOwnerLocation {
    address?: string;
    city?: string;
    state?: string;
    zip?: number;
    country?: string;
}

现在,我的响应对象看起来像这样:

{
    status: "success",
    is_error: false,
    errors: [],
    data: {
        owner: {
            lastName: "lovejoy",
            firstName: "reverend"
        }, 
        contacts: [
            {
                  name: "homer simpson",
                  address: "3 evergreen terrace, springfield, XX XX823",
                  email: "homer@springfieldnuclearpower.net"
            },
            {
                  name: "ned flanders",
                  address: "5 evergreen terrace, springfield, XX XX823",
                  email: "ned@somechurch.god"
            }
        ],
        location: {
            address: "the church",
            city: "Springfield",
            state: "XX",
            zip: XX823,
            country: "US"
        }
    }
}

请忽略我输入的 json 响应中的任何语法错误。

无论如何,我认为我需要做一些事情,将响应映射到可观察对象、管道和其他东西。到目前为止我所拥有的是:

export class SimpsonsService {
     public resourceUrl = 'www.example.com/api/simpsons';

     constructor ( protected http: HttpClient) {}

    find(name: string): Observable<EntityResponseType> {

        return this.http.get<ObjLookup>(`${this.resourceUrl}/${name}`)
           .pipe(
               map(respObj => {
                   const
               })
           );
        });
    }
}

我已经尝试了多种演绎方式,我需要提取响应对象并创建一些映射到各种接口的单独类型,然后需要将它们包含在更大的响应类型ObjLookup 中。

我需要做什么才能将适当的数据对象捕获为response.data.ownerresponse.data.contactsresponse.data.location 对象的一部分?

【问题讨论】:

    标签: angular typescript rest casting angular-cli


    【解决方案1】:

    你可以试试这个

    owner:  IObjOwner;
    contacts: IOwnerContacts[];
    location: IOwnerLocation;
    
    return this.http.get<ObjLookup>(`${this.resourceUrl}/${name}`)
               .pipe(
                   map(respObj => {
                       this.owner= respObj.data.owner
                       this.contacts= respObj.data.contacts
                       this.location= respObj.data.location
                       //do something
                   })
               );
            });
    

    【讨论】:

    • yea.it 将由返回类型推断
    • .map(respObj ) 应该返回一些东西
    • 返回在地图的什么位置
    • 关于地图,阅读这里:blog.angularindepth.com/…
    • 例如:map((response: Response) => { return response.json(); })
    【解决方案2】:

    简单的类型转换

        <IObjOwner>response.data.owner
        <Array<IOwnerContacts>>response.data.contacts
        <IOwnerLocation>response.data.location
    

    对于data wasnt a property of response,您必须将response 对象的返回类型设为any。或者使用响应对象的所有属性创建一个新接口,并将该接口的类型提供给response 对象。 说吧,

    export interface IDataResponse {
        owner?: IObjOwner;
        contacts?: Array<IOwnerContacts>;
        location?: IOwnerLocation
    }
    
    export interface IResponse {
        status?: string;
        is_error?: boolean;
        errors?: Array<any>;
        data?: IDataResponse;
    }
    

    并用作,

    owner:  IObjOwner;
    contacts: Array<IOwnerContacts> = [];
    location: IOwnerLocation;
    
    return this.http.get<ObjLookup>(`${this.resourceUrl}/${name}`)
               .pipe(
                   map((response: IResponse)=> {
                      this.owner =  <IObjOwner>response.data.owner
                      this.contacts =  <Array<IOwnerContacts>>response.data.contacts
                      this.locations = <IOwnerLocation>response.data.location
                   })
               );
       });
    

    【讨论】:

    • @BarryChapman,对不起,我正在为你的问题写解释。
    • 所以 - 你是说 Angular 要求你为 /every/ 单个 api 响应方案创建模型/类型可能有?我不相信,这将是一个疯狂的工作量。我只是想解析出一些响应对象
    • 它不是有角度的,它的打字稿要求您提供对象类型或数据类型。如果不确定类型,可以使用any
    • 我确定类型 - 我只是想确保响应对象被正确映射等等
    • @BarryChapman 请查看所有接口及其用法的新更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2018-10-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多