[英]Angular RxJS: Merging the results of 2 observables
我有 2 個 Observable,我想看看它們是否最終都是“真實的”。 雖然 http get observal 永遠不會觸發?
const storeAuth = this.store.isLoggedIn$;
const heartBeatAuth = this.http.get<boolean>('/api/auth/heartbeat');
return merge(storeAuth, heartBeatAuth).pipe(
map((a, b) => {
if (a && b) {
return true;
}
this.router.navigateByUrl('/login');
return false;
}),
take(1)
);
merge 不會發出 a 和 b 那樣的,如果您想要同時獲得兩個結果,請使用 combineLatest 。 合並將從任一流發出第一個結果,然后是下一個。
return combineLatest([storeAuth, heartBeatAuth]).pipe(
map(([a, b]) => {
combineLatest 發出一個數組,因此您可以使用[a, b]
從數組中解構 a 和 b
在訂閱返回的 observable 之前,不會觸發任何 http 請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.