Angular2 - return observable of object created in

2019-09-15 06:03发布

Say I have this function...

public getStuff(): Observable<Stuff> {
     return.http.get('url')
          .map(res => res.json())
          .map((data: Piece) => {
               var stuff = new Stuff();
               // Apply logic to "data", modify stuff model, return stuff to subscriber
          });
}

How do I return the stuff object to the observer instead of the "data" of type Piece?

1条回答
狗以群分
2楼-- · 2019-09-15 06:41

If you use a code block you need an explicit return:

public getStuff(): Observable<Stuff> {
     return.http.get('url')
          .map(res => res.json())
          .map((data: Piece) => {
               var stuff = new Stuff();
               return stuff;
          });
}
查看更多
登录 后发表回答