我无法让我的函数getData多次工作,第一次使用字段名称' a'来调用getData,从returnData和getData' s中的块返回数据.subscribe被击中。第二次使用字段' b'来调用getData时,returnData内部的所有内容都按预期工作,但是getData' s.subscribe内部的块根本没有被触发。
private getData(field: string): void {
this.returnData(field)
.subscribe((data) => {
//Handle Data
//This block is only hit the first time getData is called
});}
private returnData(field: string): Observable < SomeObj > {
let subj: Subject < SomeObj > = new Subject < SomeObj > ();
const obj: SomeObj = new SomeObj(field);
this.someDataServive.someFunction(field)
.subscribe(
(data) => {
//set properties on obj from data and emit obj
subj.next(obj);
//This block is always hit every time getData is called, and the
// obj returned from here is correct
},
(err) => {
subj.next(obj);
});
return (subj.asObservable());
}
答案 0 :(得分:0)
原来问题在于我对.subscribe以及何时对.map缺乏了解;主题也是不必要的。更改returnData对map的订阅只是在returnData中持续命中.subscribe块所需的全部内容。
private returnData(field: string): Observable < SomeObj > {
const obj: SomeObj = new SomeObj(field);
return this.someDataServive.someFunction(field)
.map(
(data) => {
obj.data = data;
return obj;
}
}