订阅流后如何返回数据? 例如,我获得订阅中的URL,然后我想返回带有基于此URD的数据的流
public get() {
this.service.getData().subscribe((data: any) => {
// data was setted here
this.url = data;
});
return this.http.get(this.url)
.map(r => {
...
return obj;
});
}
答案 0 :(得分:2)
您应该为此使用concatMap
(或mergeMap
)并将其变成一条链:
public get() {
this.service.getData().do((data: any) => {
// data was set here
this.url = data;
})
.concatMap(data => this.http.get(data))
.map(r => {
...
return obj;
});
}
或者也许您甚至不需要使用this.url
(我不知道您的用例是什么)。
public get() {
this.service.getData()
.concatMap(url => this.http.get(url))
.map(r => {
...
return obj;
});
}
答案 1 :(得分:0)
this.service.getData().subscribe((data: any) => {
// data was setted here
return this.http.get(data)
.map(r => {
return r;
});
});
或
public get() {
this.service.getData().subscribe((data: any) => {
// data was setted here
this.url = data;
this.get2(this.url);
});
}
public get2(url){
return this.http.get(url)
.map(r => {
...
return obj;
});
}
答案 2 :(得分:0)
尝试:
public get() {
this.service.getData().subscribe((data: any) => {
// data was setted here
this.url = data;
//get data with the URL
return this.http.get(data)
.map(r => {
//Get something with data obtained with the URL
return obj;
});
});
}