Angular 2 map http response to instance of class

后端 未结 3 706
鱼传尺愫
鱼传尺愫 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:32

    Good practice is to consume data from GET response using

    Observable
    

    (regarding to Angular documentation https://angular.io/guide/http) So...

    // imports

    import {HttpClient} from "@angular/common/http";
    

    // in constructor parameter list

    private http: HttpClient
    

    // service method

    getHeroes(): Observable {return this.http.get({url}, {options});}
    

    You do not need to do anything more. I consider this approach as most friendly.

提交回复
热议问题