Angular2 - 返回在map函数中创建的对象的可观察对象

时间:2016-08-02 05:24:13

标签: http angular observable

说我有这个功能......

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

如何将stuff对象返回给观察者而不是Piece类型的“数据”?

1 个答案:

答案 0 :(得分:2)

如果您使用代码块,则需要明确的return

public getStuff(): Observable<Stuff> {
     return.http.get('url')
          .map(res => res.json())
          .map((data: Piece) => {
               var stuff = new Stuff();
               return stuff;
          });
}