Angular 如何使用Rxjs解析响应?

Angular 如何使用Rxjs解析响应?,angular,rxjs,angular8,Angular,Rxjs,Angular8,我有可观察的结果作为响应数据: this.response$ = this.route.paramMap.pipe(...); 然后我需要解析这个this.response$如下: let max = this.response$ .pipe(max((a, b) => a["numberarea"] - b["numberarea"])) .subscribe(); let min = this.response$ .pipe(max((a, b)

我有可观察的结果作为响应数据:

this.response$ = this.route.paramMap.pipe(...);
然后我需要解析这个
this.response$
如下:

let max = this.response$
      .pipe(max((a, b) => a["numberarea"] - b["numberarea"]))
      .subscribe();

let min = this.response$
      .pipe(max((a, b) => b["numberarea"] - a["numberarea"]))
      .subscribe();

let avg = this.response$.pipe(...avg)
let sum = this.response$.pipe(...sum)
之后,我想将变量max、min、avg、sum作为
@Input()
传递给子组件

怎么做?如果我订阅每条语句,它会向服务器发出重复请求:

let sum = this.response$.pipe(...sum).subscribe();
...
etc
因此,我最初的来源是:
this.response$

试试这个:

this.route.paramMap.pipe(
      switchMap((params: ParamMap) =>
        of(params.get('numberarea'))
      )
    ).subscribe((res) => {
      // Do your stuff
    });

试试这个:

this.route.paramMap.pipe(
      switchMap((params: ParamMap) =>
        of(params.get('numberarea'))
      )
    ).subscribe((res) => {
      // Do your stuff
    });


您可以使用
shareReply
,而无需设置发射次数。然后,只有当
this.route.paramMap
自身发出时,才会发送一个请求,并且不会再次触发订阅

//另外'paramMap'返回一个'Map',您需要使用`.get`方法。
this.response$=this.route.paramMap.pipe(
map(param=>parseInt(param.get('numberarea'),10)),
shareReplay(),
);
//依靠每一次发射
让number=this.response$.pipe(
扫描((结果,参数)=>{
返回[…结果,参数];
}, []),
过滤器(参数=>params.length>0),
映射(参数=>({
max:Math.max(…参数),
最大值:数学最小值(…参数),
平均:参数减少((s,n)=>s+n,0)/参数长度),
).subscribe();
//或每个变量订阅或使用异步管道。
让max$=此.response$.pipe(
扫描((结果,参数)=>{
返回[…结果,参数];
}, []),
过滤器(参数=>params.length>0),
map(params=>Math.max(…params)),
);
让min$=此.response$.pipe(
扫描((结果,参数)=>{
返回[…结果,参数];
}, []),
过滤器(参数=>params.length>0),
map(params=>Math.min(…params)),
);
让avg$=此.response$.pipe(
扫描((结果,参数)=>{
返回[…结果,参数];
}, []),
过滤器(参数=>params.length>0),
映射(参数=>params.reduce((s,n)=>s+n,0)/params.length),
);
//指望订阅完成
设max=this.response$
.管道(最大值((a,b)=>a-b))
.subscribe();
让min=this.response$
.管道(最大值((a,b)=>b-a))
.subscribe();
设avg=this.response$.pipe(…avg)
设sum=this.response$.pipe(…sum)

您可以使用
shareReply
而不需要发送次数。只有当
this.route.paramMap
自身发出请求时,才会发送一个请求,并且不会再次触发订阅

//另外'paramMap'返回一个'Map',您需要使用`.get`方法。
this.response$=this.route.paramMap.pipe(
map(param=>parseInt(param.get('numberarea'),10)),
shareReplay(),
);
//依靠每一次发射
让number=this.response$.pipe(
扫描((结果,参数)=>{
返回[…结果,参数];
}, []),
过滤器(参数=>params.length>0),
映射(参数=>({
max:Math.max(…参数),
最大值:数学最小值(…参数),
平均:参数减少((s,n)=>s+n,0)/参数长度),
).subscribe();
//或每个变量订阅或使用异步管道。
让max$=此.response$.pipe(
扫描((结果,参数)=>{
返回[…结果,参数];
}, []),
过滤器(参数=>params.length>0),
map(params=>Math.max(…params)),
);
让min$=此.response$.pipe(
扫描((结果,参数)=>{
返回[…结果,参数];
}, []),
过滤器(参数=>params.length>0),
map(params=>Math.min(…params)),
);
让avg$=此.response$.pipe(
扫描((结果,参数)=>{
返回[…结果,参数];
}, []),
过滤器(参数=>params.length>0),
映射(参数=>params.reduce((s,n)=>s+n,0)/params.length),
);
//指望订阅完成
设max=this.response$
.管道(最大值((a,b)=>a-b))
.subscribe();
让min=this.response$
.管道(最大值((a,b)=>b-a))
.subscribe();
设avg=this.response$.pipe(…avg)
设sum=this.response$.pipe(…sum)

要防止不必要的请求,请在创建可观察的
响应时使用
shareReplay
操作符:

response$ = ...pipe(
  // other pipes
  shareReplay(1)
)
要将值从可观测传递到子组件中,您应该创建可观测的最大值、最小值等

max$ = this.response$
      .pipe(
         map(
           arrayOfItems => 
             arrayOfItems.sort(
               (a, b) => a["numberarea"] - b["numberarea"]
             )[0]
         )
       )
并将其与
async
管道一起在模板中使用

[max]="max$ | async"
该管道接管订阅处理。但在值未结算时,向子组件输入提供
null


*ngIf
指令起到了解救作用:当值为falsy(“”、0、null等)时,子组件不会初始化。

为了防止不必要的请求,请在创建
响应时使用
shareReplay
操作符$
可观察:

response$ = ...pipe(
  // other pipes
  shareReplay(1)
)
要将值从可观测传递到子组件中,您应该创建可观测的最大值、最小值等

max$ = this.response$
      .pipe(
         map(
           arrayOfItems => 
             arrayOfItems.sort(
               (a, b) => a["numberarea"] - b["numberarea"]
             )[0]
         )
       )
并将其与
async
管道一起在模板中使用

[max]="max$ | async"
该管道接管订阅处理。但在值未结算时,向子组件输入提供
null


*ngIf
指令起到了解救作用:当值为false(“”、0、null等)时,子组件将不会初始化

这不是我所期望的,我想在一个蒸汽中实现这一点。我需要将rxjs应用于
response$
并创建另一个观察对象,如mix,max,Avg。这不是我所期望的,我想在一个蒸汽中实现这一点。我需要将rxjs应用于
response$
并创建另一个观察对象,如mix,max,avgAh,我明白了,对于问题,然后
shareReplay
肯定是更好的解决方案。另外,我用
share
操作员的信息更新了答案,也许它更适合。啊哈,那么我想你需要
shareReplay()
无参数。请检查更新后的答案。@satanTime:有关
max
的信息,请参见此处操作员:谢谢!我想问题是,它将max计算在完成时,这是期望的行为吗?然后只需使用
此.response$
定义,而不使用我答案的第二部分,它应该这样做。啊,我明白了,不清楚对于这个问题,那么
shareReplay
肯定是更好的解决方案。另外,我已经用
share
操作符的信息更新了答案,也许它更适合。啊哈,那么我认为您需要
shareReplay()
而不带参数。请检查更新后的答案。@satanTime:有关详细信息,请参阅此处