[英]How to avoid nested subscriptions with concatMap in Angular
I get an array of products with this code我用这段代码得到了一系列产品
proyecto$:Observable<Proyecto>
this.proyecto$=this.http.get<ProjectApi>(`${this.urlProyecto}/${this.proyectoId}`)
.pipe(
map(p => <Proyecto>{
id: p.id,
descripcion: p.name,
fechaComienzoTrabajos: p.workStartDate,
fechaHistorico: p.historicoDate,
fechaFinPlanificacion: p.planningEndDate,
proyectoCerrado: p.projectClosed,
faseCerrada: p.phaseClosed,
tieneHijos: p.tieneHijos
}),
tap(data=>console.log('Proyecto: ',JSON.stringify(data))),
catchError(this.handleError)
);
But I need now to get the Hours charged in this project in function of its properties:但我现在需要在其属性的 function 中获取该项目的收费小时数:
That is, I need something like this也就是说,我需要这样的东西
But I get an error cause concatMap returns an Observable<Horas[]> and proyecto$ is an Observable但是我得到一个错误,因为 concatMap 返回一个 Observable<Horas[]> 并且 proyecto$ 是一个 Observable
If I try this如果我试试这个
horas$:Observable<Horas[]>
this.horas$=this.proyecto$=this.http.get<ProjectApi>(`${this.urlProyecto}/${this.proyectoId}`)
.pipe(
map(p => <Proyecto>{
id: p.id,
descripcion: p.name,
fechaComienzoTrabajos: p.workStartDate,
fechaHistorico: p.historicoDate,
fechaFinPlanificacion: p.planningEndDate,
proyectoCerrado: p.projectClosed,
faseCerrada: p.phaseClosed,
tieneHijos: p.tieneHijos,
}),
tap(data=>console.log('Proyecto: ',JSON.stringify(data))),
concatMap(proyecto=>this.http.get<Horas[]>(`${this.urlProyecto}/fechainicio/${proyecto.fechaComienzoTrabajos}/fechafin/${proyecto.fechaFinPlanificacion}/horas`)),
catchError(this.handleError)
);
I get error in both variables我在两个变量中都出现错误
If I try this如果我试试这个
this.proyecto$=this.http.get<ProjectApi>(`${this.urlProyecto}/${this.proyectoId}`)
.pipe(
map(p => <Proyecto>{
id: p.id,
descripcion: p.name,
fechaComienzoTrabajos: p.workStartDate,
fechaHistorico: p.historicoDate,
fechaFinPlanificacion: p.planningEndDate,
proyectoCerrado: p.projectClosed,
faseCerrada: p.phaseClosed,
tieneHijos: p.tieneHijos,
}),
tap(data=>console.log('Proyecto: ',JSON.stringify(data))),
catchError(this.handleError)
);
this.horas$=this.proyecto$
.pipe(
concatMap(proyecto=>this.http.get<Horas[]>(`${this.urlProyecto}/fechainicio/${proyecto.fechaComienzoTrabajos}/fechafin/${proyecto.fechaFinPlanificacion}/horas`)),
tap(data=>console.log('Horas: ',JSON.stringify(data)))
);
I get nothing in his.horas$ in fact the http.get it is not carried out我在 his.horas$ 中一无所获,实际上 http.get 没有执行
How can I assign the value returned from concatMap in another observable variable?如何将 concatMap 返回的值分配给另一个可观察变量?
Thanks谢谢
create another variable like创建另一个变量,例如
horas$: Observable<Horas[]>;
this.horas$ = this.proyecto$
.pipe(
concatMap(proyecto => ...)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.