我是 RxJS 的初学者,只熟悉基础知识。 情况是: 我有一个可观察的对象,它应该根据需要异步加载一些数据。另一个函数应该创建该需求并要求第一个可观察的加载下一条数据。
像这样:
dataPiece$:Observable<DataPiece> = http.load(this.url + this.pieceUrl)
loadNextPiece(pieceUrl)
{
this.pieceUrl = pieceUrl;
//make the observable to automatically load new piece of data
//once pieceUrl is updated
}
最佳答案
您可以使用Subject创建包含pieceUrls的流。 。对于每件作品,都将发出请求并使用 switchMap您可以将所有响应合并到一个新流中。
let urlPiece = new Subject();
let dataPiece$:Observable<DataPiece>;
loadNextPiece(pieceUrl)
{
urlPiece.next(pieceUrl);
}
dataPiece$ = urlPiece.asObservable()
.switchMap(urlPiece => http.load(this.url + pieceUrl))
.map((response) => {
// parse response or just return it
return response;
})
.catch(error => {
// handle error
});
let subscription = dataPiece$.subscribe( (response) => {
//do something with response
})
关于javascript - 从外部可以观察到 RxJS 的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41555200/