Rxjs 将api请求的返回结果与Forkjoin、mergeMap和管道一起使用
我想使用来自多个api请求的返回结果,并将其作为第二个api请求的参数重用,第二个api请求返回我必须在第三个api请求中重用的结果 问题是我只能返回一个apiRxjs 将api请求的返回结果与Forkjoin、mergeMap和管道一起使用,rxjs,pipe,observable,fork-join,mergemap,Rxjs,Pipe,Observable,Fork Join,Mergemap,我想使用来自多个api请求的返回结果,并将其作为第二个api请求的参数重用,第二个api请求返回我必须在第三个api请求中重用的结果 问题是我只能返回一个api const email = this.credentialsForm.controls['email'].value; this.http.get('https://drupal_users/index/' + email).pipe( map(users => { console.lo
const email = this.credentialsForm.controls['email'].value;
this.http.get('https://drupal_users/index/' + email).pipe(
map(users => {
console.log(users);
const user = users;
this.userId = user;
return user;
}),
mergeMap(user => {
const ObservateurCulture = this.http.get('https://index.php/drupal_users/culture/' + email).subscribe((culture: any) => {})
// Here i wish to reuse "culture" data in another Api call and reuse result again of "Parcelles" api request
const Parcelles = this.http.get('https://index.php/drupal_users/totherdata/' + culture);
return forkJoin([ObservateurCulture, Parcelles]);
}),
take(1)
).subscribe(result => {
console.log(result);
this.loading.dismiss();
});
如果您只想从上一个api中获得结果,
mergeMap
对于所有事情来说都足够了
但是,如果您希望从所有API组合所有数据,则必须另外使用pipe
和map
下面是示例片段
让userId=-1;
const email='test';
const getUser=(电子邮件)=>rxjs.of(1)
const getCulture=(email)=>rxjs.of('en-US')
const getData=(区域性)=>rxjs.of({
数据:{}
})
const getResultFromLastApi=(电子邮件)=>{
getUser(电子邮件).pipe(
rxjs.operators.tap(userId=>{
userId=userId;
}),
rxjs.operators.mergeMap(userId=>getCulture(email)),
rxjs.operators.mergeMap(区域性=>getData(区域性)),
rxjs.operators.take(1)
).订阅(结果=>{
控制台日志(结果);
});
}
const getCombinedResult=()=>{
getUser(电子邮件).pipe(
rxjs.operators.tap(userId=>{
userId=userId;
}),
mergeMap(userId=>getCulture(email.pipe)(rxjs.operators.map(culture=>({
用户ID,
文化
})))),
mergeMap(cultureAndUser=>getData(cultureAndUser.culture).pipe(rxjs.operators.map)(数据=>({…数据,
…文化达瑟
})))),
rxjs.operators.take(1)
).订阅(结果=>{
控制台日志(结果);
});
}
getResultFromLastApi(电子邮件);
getCombinedResult(电子邮件)代码>
如果您只想从上一个api中获得结果,mergeMap
对于所有事情来说都足够了
但是,如果您希望从所有API组合所有数据,则必须另外使用pipe
和map
下面是示例片段
让userId=-1;
const email='test';
const getUser=(电子邮件)=>rxjs.of(1)
const getCulture=(email)=>rxjs.of('en-US')
const getData=(区域性)=>rxjs.of({
数据:{}
})
const getResultFromLastApi=(电子邮件)=>{
getUser(电子邮件).pipe(
rxjs.operators.tap(userId=>{
userId=userId;
}),
rxjs.operators.mergeMap(userId=>getCulture(email)),
rxjs.operators.mergeMap(区域性=>getData(区域性)),
rxjs.operators.take(1)
).订阅(结果=>{
控制台日志(结果);
});
}
const getCombinedResult=()=>{
getUser(电子邮件).pipe(
rxjs.operators.tap(userId=>{
userId=userId;
}),
mergeMap(userId=>getCulture(email.pipe)(rxjs.operators.map(culture=>({
用户ID,
文化
})))),
mergeMap(cultureAndUser=>getData(cultureAndUser.culture).pipe(rxjs.operators.map)(数据=>({…数据,
…文化达瑟
})))),
rxjs.operators.take(1)
).订阅(结果=>{
控制台日志(结果);
});
}
getResultFromLastApi(电子邮件);
getCombinedResult(电子邮件)代码>