Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular RxJS根据以前的结果订阅多个http请求的最佳实践大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道使用RxJS库执行3个http请求的最佳方法是什么,这些请求取决于之前的结果.

让我们假设我的Angular应用程序中有3个服务,每个服务都有一个函数get(id:number)用于订阅请求实体的可观察对象.

我需要通过使用第二个服务@L_675_4@第一个服务的顺序来获取包含下一个@L_675_4@所需的标识符的实体,该第二个服务还包含使用第三个服务进行下一次@L_675_4@所需的标识符.

方法1:使用三个订阅并将每个结果设置为全局变量

@H_944_13@const firstEntityId = 1; this.firstservice.get(firstEntityId) .subscribe((firstEntity: FirstEntity) => { this.firstEntity = firstEntity; this.secondservice.get(firstEntity.secondEntityId) .subscribe((secondEntity: SecondEntity) => { this.secondEntity = secondEntity; this.thirdservice.get(secondEntity.thirdEntityId) .subscribe((thirdEntity: ThirdEntity) => { this.thirdEntity = thirdEntity; }); }); });

方法2:使用带有流和一个订阅函数来设置所有全局变量

@H_944_13@const firstEntityId = 1; this.getFirstSecondThird(firstEntityId) .subscribe(([firstEntity,secondEntity,thirdEntity]: [FirstEntity,SecondEntity,ThirdEntity]) => { this.firstEntity = firstEntity; this.secondEntity = secondEntity; this.thirdEntity = thirdEntity; }); getFirstSecondThird(id: number): Observable<[FirstEntity,ThirdEntity]> { return this.firstservice.get(id).pipe( switchMap((firstEntity: FirstEntity) => forkJoin( of(firstEntity),this.secondservice.get(firstEntity.secondEntityId) )),switchMap(([firstEntity,secondEntity]: [FirstEntity,SecondEntity]) => forkJoin( of(firstEntity),of(secondEntity),this.thirdservice.get(secondEntity.thirdEntityId) )) ); }

在这种情况下,使用流的方法是最快的吗?

有没有其他方法来编写我的函数getFirstSecondThird而不是使用switchMap和forkJoin方法

(我见过combineLatest,但我没有找到如何传递前一个结果中的参数)

@H_944_27@解决方法
也许在方法1中使用map而不是订阅

注意,您需要在所有嵌套级别返回.在示例中,我删除了括号,因此隐含了返回.

@H_944_13@getFirstSecondThird(id: number): Observable<[FirstEntity,ThirdEntity]> { return this.firstservice.get(id).pipe( mergeMap((first: FirstEntity) => this.secondservice.get(first.secondEntityId).pipe( mergeMap((second: SecondEntity) => this.thirdservice.get(second.thirdEntityId).pipe( map((third: ThirdEntity) => [first,second,third]) ) ) ) ) ) }

这是一个测试片段,

@H_944_13@console.clear() const { interval,of,fromEvent } = rxjs; const { expand,take,map,mergeMap,tap,throttleTime } = rxjs.operators; const firstservice = (id) => of(1) const secondservice = (id) => of(2) const thirdservice = (id) => of(3) const getFirstSecondThird = (id) => { return firstservice(id).pipe( mergeMap(first => secondservice(first.secondEntityId).pipe( mergeMap(second => thirdservice(second.thirdEntityId).pipe( map(third => [first,third]) ) ) ) ) ) } getFirstSecondThird(0) .subscribe(result => console.log('result',result)) @H_944_13@<script src="https://cdnjs.cloudFlare.com/ajax/libs/rxjs/6.3.3/rxjS.UR_812_11845@d.js"></script>

你可以使用switchMap()而不是mergeMap(),如果有可能第二次@L_675_4@getFirstSecondThird()但是在第一次@L_675_4@的所有提取完成之前,你想要丢弃第一个@L_675_4@ – 例如在增量搜索方案.

大佬总结

以上是大佬教程为你收集整理的Angular RxJS根据以前的结果订阅多个http请求的最佳实践全部内容,希望文章能够帮你解决Angular RxJS根据以前的结果订阅多个http请求的最佳实践所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。