使用rxjs的异步管道 [英] Async pipe with rxjs
本文介绍了使用rxjs的异步管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在异步管道中有一个小问题这是我的情况,我需要在html的异步管道中运行嵌套的observables,因为我使用了推策略,并且我不想使用某些解决方法或更改检测器引用.我的问题是,当我在下面运行代码时,仅第一个可观察到的被调用我应该添加返回语句吗?还是什么问题?
I have a little problem in async pipe Here is my case , I need to run nested observables in async pipe in html because i use on push strategy and i dont want to use some workarounds or change detector reference . My problem is , when i run the code below only the first observable is called Should i add return statements? Or whats the problem ?
Ts代码
this.http.getUsers(criteria)
.pipe(map(data=>{
data.users.map(user=>{
this.http.getUserData(user.id)
.pipe(map(res=>{user.data=res.data}))}}
HTML代码
< div * ngFor =让用户成为用户$ |异步></div>
推荐答案
您要创建一个switchMap,并且需要将observable分配给users $属性.
You want to do a switchMap and you need to assign an observable to the users$ property.
users$ = this.http.getUsers(criteria).pipe(
switchMap(user => this.http.getUserData(user.id)),
map(res => res.data)
);
这篇关于使用rxjs的异步管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文