Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http Angular2-返回在映射函数中创建的对象的可观测值_Http_Angular_Observable - Fatal编程技术网

Http Angular2-返回在映射函数中创建的对象的可观测值

Http Angular2-返回在映射函数中创建的对象的可观测值,http,angular,observable,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

假设我有这个功能

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
          });
}
public getStuff():可观察{
return.http.get('url')
.map(res=>res.json())
.map((数据:件)=>{
var stuff=newstuff();
//对“数据”应用逻辑,修改材料模型,将材料返回给订阅者
});
}

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

如果使用代码块,则需要显式返回:

public getStuff(): Observable<Stuff> {
     return.http.get('url')
          .map(res => res.json())
          .map((data: Piece) => {
               var stuff = new Stuff();
               return stuff;
          });
}
public getStuff():可观察{
return.http.get('url')
.map(res=>res.json())
.map((数据:件)=>{
var stuff=newstuff();
归还物品;
});
}

退货在第二个map函数中,按您的要求执行吗?如果没有,有什么不同?