Angular 使用NGRX&;有棱角的 ngOnInit():void{ this.store.dispatch(新患者.Load([]); this.patients$=this.store.select(fromPatients.getAll); 这个.patients$.map(p=>{//patients$:可观察; this.rows=p.map(pat=>{//我在前端使用它 返回{ 用户名:pat.username, id:pat.id, hasAlert:this.hasAlerts(pat),//使其成为视图中异步管道的可观察对象 设置:“设置” }; }); this.table.recomparePages(); console.log(this.rows); log(“这首先发生”); }).subscribe(); } hasAlerts(pat:患者):可观察到{ var shouldAlert$=Observable.of(false);//始终采用此值 this.observations2$=this.dataService.fetchItems( “观察”, 无效的 pat.id//mergeMap()如何传递此信息? ); this.observations2$.subscribe(curobservations=>{ if(curObservation.category.coding[0].code==“GlucoseEvent”){ shouldAlert$=可观察的(真); log(“应警报$”,shouldAlert$); } }); log(“这是第二次发生”); 返回shouldAlert$; }
在上面的代码中,我解析了一个名为patients$的可观察对象,它有一个patients数组。然后,我将这些患者映射到我在客户端上显示的名为this.rows的对象数组 我的问题涉及到hasAlert属性,它在对hasAlerts()的方法调用中处理另一个可观察到的自身。对hasAlerts()的方法调用不是同步发生的,因此console.log(“这首先发生”);在我的hasAlerts方法执行If语句中的逻辑以决定是否应将其设置为true或false之前发生,它只使用在hasAlerts()的第一行中初始化为的值。由console.log确认(“这是第二次发生”);第二个被显示 hasAlerts()可以返回一个布尔值,而不是一个可观察值,我试图通过在前端使用asycn管道来看看这是否能解决我的问题(它没有) 我相信解决这个问题的方法包括使用mergemap,但是我不确定如何传入hasAlerts方法需要的pat.id?或者,这可能不是解决当前代码异步执行问题的正确方法 我目前正试图用它来解决我的问题,但传递的pat.id是hasAlerts中第二个可观察到的,我还没有弄清楚 根据Piccis的想法更新了代码。Angular 使用NGRX&;有棱角的 ngOnInit():void{ this.store.dispatch(新患者.Load([]); this.patients$=this.store.select(fromPatients.getAll); 这个.patients$.map(p=>{//patients$:可观察; this.rows=p.map(pat=>{//我在前端使用它 返回{ 用户名:pat.username, id:pat.id, hasAlert:this.hasAlerts(pat),//使其成为视图中异步管道的可观察对象 设置:“设置” }; }); this.table.recomparePages(); console.log(this.rows); log(“这首先发生”); }).subscribe(); } hasAlerts(pat:患者):可观察到{ var shouldAlert$=Observable.of(false);//始终采用此值 this.observations2$=this.dataService.fetchItems( “观察”, 无效的 pat.id//mergeMap()如何传递此信息? ); this.observations2$.subscribe(curobservations=>{ if(curObservation.category.coding[0].code==“GlucoseEvent”){ shouldAlert$=可观察的(真); log(“应警报$”,shouldAlert$); } }); log(“这是第二次发生”); 返回shouldAlert$; },angular,rxjs,ngrx,Angular,Rxjs,Ngrx,在上面的代码中,我解析了一个名为patients$的可观察对象,它有一个patients数组。然后,我将这些患者映射到我在客户端上显示的名为this.rows的对象数组 我的问题涉及到hasAlert属性,它在对hasAlerts()的方法调用中处理另一个可观察到的自身。对hasAlerts()的方法调用不是同步发生的,因此console.log(“这首先发生”);在我的hasAlerts方法执行If语句中的逻辑以决定是否应将其设置为true或false之前发生,它只使用在hasAlerts()
ngOnInit(): void {
this.store.dispatch(new patients.Load([]));
this.patients$ = this.store.select(fromPatients.getAll);
this.patients$.map(p =>{ // patients$: Observable<Patient[]>;
this.rows = p.map(pat => { //I use this on the front end
return {
username: pat.username,
id: pat.id,
hasAlert: this.hasAlerts(pat), //made this an observable for the async pipe in view
settings: "Settings"
};
});
this.table.recalculatePages();
console.log(this.rows);
console.log("This happens first");
}).subscribe();
}
hasAlerts(pat: Patient): Observable<boolean> {
var shouldAlert$ = Observable.of(false);//this value is always taken
this.observations2$ = this.dataService.fetchItems<Observation>(
"Observation",
null,
pat.id// How would i pass this in with mergeMap()?
);
this.observations2$.subscribe(curObservation => {
if (curObservation.category.coding[0].code == "GlucoseEvent"){
shouldAlert$ = Observable.of(true);
console.log("should alert$", shouldAlert$);
}
});
console.log("this happens second");
return shouldAlert$;
}
this.patients$.map(p=>{//patients$:可观察;
this.rows=p.map(pat=>{//我在前端使用它
返回{
用户名:pat.username,
id:pat.id,
hasAlert:false,//设置默认值
设置:“设置”
};
})
}).do(数据=>console.log(“数据1”,数据))
//.switchMap(p=>Observable.from(p))
//.do(data=>console.log(“data2”,data))//创建可观察数据流
//.mergeMap(患者=>this.dataService.fetchItems(
//“观察”,
//空,
//“帕特..弗兰克”//patient[0].id//“帕特..弗兰克”//patient.id///patient应该是你的人
// )
//.map(curObservation=>{
//console.log(“currOBS”,curObservation);
//if(curObservation.category.coding[0]。code==“GlucoseEvent”){
//var shouldAlert$=true;
//log(“应警报$”,shouldAlert$);
// }
// })
//).do(数据=>console.log(数据))
//.toArray()
.订阅(
患者=>{
this.table.recomparePages();
console.log(this.rows);
}
)
Data1返回一个患者数组。我需要注释中间部分,因为switchmap行有一个语法错误,它说“类型为'void'的参数不能赋值给类型为'ArrayLike'的参数”您使用
mergeMap
查看的方向是正确的,但您需要重新编写一点代码
您可以从this.patients$开始,这是一个可观察的变量
从这里您需要创建一个可观察的流。这可以通过可观测的中的静态方法实现
现在,您可以管理每个患者,并通过服务fetchItems
获取他/她的curObservation
最终,您将重新创建阵列,然后订阅
最终的结果可能会像这样
this.patients$.map(p =>{ // patients$: Observable<Patient[]>;
this.rows = p.map(pat => { //I use this on the front end
return {
username: pat.username,
id: pat.id,
hasAlert: false, //set the default value
settings: "Settings"
};
})
}).do(data => console.log("data1",data))
// .switchMap(p => Observable.from(p))
// .do(data => console.log("data2",data)) // creates a stream of Observable<Patient>
// .mergeMap(patient => this.dataService.fetchItems<Observation>(
// "Observation",
// null,
// "pat..frank"//patient[0].id//"pat..frank"//patient.id// patient should be your guy
// )
// .map(curObservation => {
// console.log("currOBS",curObservation);
// if (curObservation.category.coding[0].code == "GlucoseEvent"){
// var shouldAlert$ = true;
// console.log("should alert$", shouldAlert$);
// }
// })
// ).do(data => console.log(data))
// .toArray()
.subscribe(
patients => {
this.table.recalculatePages();
console.log(this.rows);
}
)
基本问题是组合两个异步调用,可以使用zip()
完成
(注意,我最初发布了一个带有forkJoin()
的解决方案,但这不适用于ngrx select(),因为选择永远不会完成-因此forkJoin永远不会触发)
将从第一次提取返回的可观察的
转换为可观察的
,因为这对zip操作符更方便
下一个问题是第二个异步依赖于第一个异步的结果(pat.id)-使用concatMap()
构建该异步
(注意,我最初建议mergeMap()
,但是concatMap()
保证hasAlert$的顺序与患者$相同。这一点很重要,因为This.dataService.fetchItems()
可能会返回顺序不正确的单个回迁)
从'rxjs/observable/zip'导入{zip};
...
ngOnInit():void{
this.store.dispatch(新患者.Load([]);
const patient$=this.store.select(from patients.getAll)
.mergeMap(value=>value);//从可观察转换为可观察
const hasAlert$=患者$.concatMap(患者=>{
返回this.dataService.fetchItems('Observation'null,patient.id)
.map(curObservation=>curObservation.category.coding[0]。代码=='GlucoseEvent')
);
})
zip(patient$,hasAlert$)//组合来自两个asyn的值
ngOnInit(): void {
this.store.dispatch(new patients.Load([]));
this.patients$ = this.store.select(fromPatients.getAll);
this.patients$.map(p =>{ // patients$: Observable<Patient[]>;
this.rows = p.map(pat => { //I use this on the front end
return {
username: pat.username,
id: pat.id,
hasAlert: false, //set the default value
settings: "Settings"
};
});
})
.do(data => consoel.log(data)) // check whether data is an array of Patients as it is supposed to be
.switchMap(p => Observable.from(p)) // creates a stream of Observable<Patient>
.do(data => console.log(data)) // check whether data is a single Patient
.mergeMap(patient => this.dataService.fetchItems<Observation>(
"Observation",
null,
patient.id// patient should be your guy
)
.map(curObservation => {
if (curObservation.category.coding[0].code == "GlucoseEvent"){
shouldAlert$ = true;
console.log("should alert$", shouldAlert$);
}
})
)
.toArray()
.subscribe(
patients => {
this.table.recalculatePages();
console.log(this.rows);
}
)
}
import {Observable} from 'rxjs';
const obs1 = Observable.of([1, 2, 3, 4, 5, 6]);
obs1
.switchMap(n => Observable.from(n))
.mergeMap(n => Observable.of(n*2))
.toArray()
.subscribe(console.log)
ngOnInit(): void {
this.store.dispatch(new patients.Load([]));
this.patients$ = this.store.select(fromPatients.getAll);
this.patients$.map(p =>{ // patients$: Observable<Patient[]>;
this.rows = p.map(pat => { //I use this on the front end
var rowx= {
username: pat.username,
id: pat.id,
hasAlert: false, //made this an observable for the async pipe in view
settings: "Settings"
};
this.hasAlerts(pat).do(x => {
observations++;
if (observations>0)
{
rowX.hasAlert=true;
}
}).subscribe();
return rowX;
});
}).subscribe(
()=>{
},
()=>{
this.table.recalculatePages();
});
}
hasAlerts(pat: Patient): Observable<Observation> {
var obs$ = this.dataService.fetchItems<Observation>(
"Observation",
null,
pat.id
).filer(function(curObservation){
if (curObservation.category.coding[0].code == "GlucoseEvent"){
return true;
}
else{
return false;
}
});
return obs$;
}