如何使用可观察的水龙头

时间:2018-12-05 23:32:57

标签: angular rxjs

我通过flatMap获取用户详细信息,还需要获取用户图像字节流:

this.authStore.jwtContainer$.pipe(
    tap(j => {
        foo = this.userService.getUserImageDataUrl(j.userImageObjectKey).subscribe(i => foo = i);
        console.log(j);
    }),
    flatMap(x => this.userService.getUserById(x.userLoginId)))
    .subscribe(z => this.user = z);

此代码有效,但我似乎遇到了双重订阅的情况,是否有办法让两个调用都基于jwtContainer订阅触发异步?

1 个答案:

答案 0 :(得分:1)

是的。您可以使用forkJoin组合所有可观察物,并使其平行发射:

if 'statusCode' in tree_root.decode() : 
    print(ticket)
    continue
etree = ET.fromstring(ticket_page.content.decode())    
print(etree)

请注意,this.authStore.jwtContainer$.pipe( flatMap(x => forkJoin([ this.userService.getUserImageDataUrl(x.userImageObjectKey), this.userService.getUserById(x.userLoginId)]))) .subscribe(([image,user]) => this.user = user); 必须等待其所有可观测值完成后才能发出错误的最终汇总结果。