Casting a type (interface) via map to observable

后端 未结 3 1106
野的像风
野的像风 2020-12-19 05:11

I\'ve been learning TypeScript with Angular. And currently I stuck as this moment. Previously I used subscribed method and everything works flawlessly, but not I decided to

3条回答
  •  攒了一身酷
    2020-12-19 05:50

    Why not directly return an Observable from your service?

    getRestaurant(): Observable {
      return this.http.get(environment.api + "/restaurant")
        .map((response: responseFormat) => response.data as restaurant);
    }
    

    Then on component side:

    getRestaurant() {
      this.restaurantProvider.getRestaurant().subscribe((res: restaurant) => {
        this.restaurant = res;
      });
    }
    

    The error handling (success flag, HTTP errors, etc) could be handled via an HTTP Interceptor.

提交回复
热议问题