[英]RxJS influence observable from outside
我是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創建一個包含pieceUrl的流。 每一件都會發出請求,使用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
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.