Angular 在subscribe中调用subscribe的最佳方式是什么?
我需要调用API两次,首先找到密钥,然后用该密钥提取相关数据 我通过在subscribe中订阅来实现这一点,但发现这是一种糟糕的编程实践。我读过关于flapmap的文章,但是还没有正确地实现它 ``打字稿Angular 在subscribe中调用subscribe的最佳方式是什么?,angular,typescript,angular8,Angular,Typescript,Angular8,我需要调用API两次,首先找到密钥,然后用该密钥提取相关数据 我通过在subscribe中订阅来实现这一点,但发现这是一种糟糕的编程实践。我读过关于flapmap的文章,但是还没有正确地实现它 ``打字稿 this.DrQue.clinicDoctorQueControllerFind('key', ({ "include": [{"relation":"patientQue"}] })).subscribe(data => { this.drQue = data;
this.DrQue.clinicDoctorQueControllerFind('key', ({ "include": [{"relation":"patientQue"}] })).subscribe(data => {
this.drQue = data;
for (let i = 0; i < this.drQue.length; ++i) {
if (this.componentDr == this.drQue[i].name) {
this.indexDr = i;
i = this.drQue.length + 1;
}
}
this.patQue.patientQueCheckInControllerFind(this.drQue[this.indexDr].patientQue.id).subscribe(data => {
for (let count = 0; count < data.length; ++count) {
if (data[count].status == this.checkStatus){
this.checkInArr.push(data[count]);
}
}
});
});
this.DrQue.clinicDoctorQueControllerFind('key',({“include”:[{“relation”:“patientquee”}]})).subscribe(数据=>{
this.drQue=数据;
for(设i=0;i{
for(让计数=0;计数
``试试这个:
this.DrQue.clinicDoctorQueControllerFind('key', ({ "include": [{"relation":"patientQue"}] }))
.pipe(
switchMap((data) => {
/**
* Here we manipulate our data
*/
this.drQue = data;
for (let i = 0; i < this.drQue.length; ++i) {
if (this.componentDr == this.drQue[i].name) {
this.indexDr = i;
i = this.drQue.length + 1;
}
}
/**
* Here we return new Observable stream
*/
return this.patQue.patientQueCheckInControllerFind(this.drQue[this.indexDr].patientQue.id);
})
).subscribe(data => {
/**
* Here we subscribe to the result and manipulate it
*/
for (let count = 0; count < data.length; ++count) {
if (data[count].status == this.checkStatus){
this.checkInArr.push(data[count]);
}
}
});
this.DrQue.clinicDoctorQueControllerFind('key',({“include”:[{“relation”:“patientQueue”}]}))
.烟斗(
开关映射((数据)=>{
/**
*在这里,我们操纵我们的数据
*/
this.drQue=数据;
for(设i=0;i{
/**
*在这里,我们订阅结果并对其进行操作
*/
for(让计数=0;计数
一般来说,rxjs操作员是此类用例的朋友
在此处查找文档:
我建议在这种情况下使用switchMap和tap
this.DrQue.clinicDoctorQueControllerFind('key', ({ "include": [{"relation":"patientQue"}] })).pipe(
tap(data => {
this.drQue = data;
for (let i = 0; i < this.drQue.length; ++i) {
if (this.componentDr == this.drQue[i].name) {
this.indexDr = i;
i = this.drQue.length + 1;
}
}
}),
switchMap(data => this.patQue.patientQueCheckInControllerFind(this.drQue[this.indexDr].patientQue.id)),
tap(data => {
for (let count = 0; count < data.length; ++count) {
if (data[count].status == this.checkStatus){
this.checkInArr.push(data[count]);
}
})
).subscribe();
this.DrQue.clinicDoctorQueControllerFind('key',({“include”:[{“relation”:“patientQueue”}]})).pipe(
点击(数据=>{
this.drQue=数据;
for(设i=0;ithis.patQue.patientQueCheckInControl智能驭享查找(this.drQue[this.indexDr].patientQuee.id)),
点击(数据=>{
for(让计数=0;计数
您可以根据需要使用或其他地图,而不是创建嵌套订阅。