Angular2 - return observable of object created in map function

删除回忆录丶 提交于 2019-12-12 02:11:02

问题


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:


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;
          });
}


来源:https://stackoverflow.com/questions/38712303/angular2-return-observable-of-object-created-in-map-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!