我目前有一項服務,我可以在其中獲取一些模板,并在展開的幫助下將它們顯示在視圖中。這個想法是繼續獲取??,直到我擁有一切。
但是,當我通過管道擴展它時,它會替換值而不是添加它們,我該如何解決這個問題?
例子:
templates$: Observable<Template[]>;
let count = 0;
this.templates$ = this.templateService.getTemplates(0, 5).pipe(
expand(result => {
count = result.length;
if (result.length === 5) {
return this.templateService.getTemplates(count, 5);
} else {
return empty();
}
})
);
uj5u.com熱心網友回復:
expand不會替換值,而是在您每次收到五個一組的模板時發出。在您看來,如果您使用template$ | async,您只會看到最后的結果。
要收集所有模板,您可以使用scan運算子。
templates$: Observable<Template[]>;
let count = 0;
this.templates$ = this.templateService.getTemplates(0, 5).pipe(
expand(result => {
count = result.length;
if (result.length === 5) {
return this.templateService.getTemplates(count, 5);
} else {
return empty();
}
}),
scan((acc, curr) => acc.concat(curr))
);
reduce如果您不想顯示中間結果,您也可以以相同的方式使用運算子。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349616.html
