Angular 2 map http response to instance of class

后端 未结 3 722
鱼传尺愫
鱼传尺愫 2021-02-19 05:57

I\'m wondering what the best way is to map the http response from a get request to a class instead of a basic Javascript object.

In my current attempt I simple do

3条回答
  •  星月不相逢
    2021-02-19 06:45

    I think that you could use the map method of JavaScript objects:

    getHeroes () {
      return this.http.get(this._heroesUrl)
                .map(res => {
                   return res.json().data.map((elt) => {
                     // Use elt to create an instance of Hero
                     return new Hero(...);
                   });
                 })
                .catch(this.handleError);
    }
    

提交回复
热议问题