Angular 使用NgBusy与行为主体和排气图

Angular 使用NgBusy与行为主体和排气图,angular,Angular,在行为主体上使用更复杂的RxJS运算符(如开关映射或排气映射)时,最好的做法是什么,而不订阅订阅块中的可观察对象 如果在BehaviorSubject上设置this.busy,它将始终处于忙碌状态,因为它将离开BehaviorSubject而不是Http请求订阅 public currentPage$ = new BehaviorSubject(1); // ... // Not sure how to use "busy" this way: this.currentPage$ .pi

行为主体
上使用更复杂的RxJS运算符(如
开关映射
排气映射
)时,最好的做法是什么,而不订阅订阅块中的可观察对象

如果在
BehaviorSubject
上设置this.busy,它将始终处于忙碌状态,因为它将离开
BehaviorSubject
而不是Http请求订阅

public currentPage$ = new BehaviorSubject(1);

// ...

// Not sure how to use "busy" this way:
this.currentPage$
  .pipe(
    exhaustMap((page: number = 1) =>
      (page) ? this.getAlerts(page) : empty()
    ),
    map((response: HttpResponse<Alert[]>) => 
      response.results
    )
  )
  .subscribe(
    (alerts: Alert[]) => { ... },
    (error: any) => { ... }
  );
public currentPage$=新行为主体(1);
// ...
//不确定如何以这种方式使用“忙”:
此为.currentPage$
.烟斗(
排气图((页码=1)=>
(第页)?this.getAlerts(第页):空()
),
map((response:HttpResponse)下面的代码在使用Observable时不是最佳实践,但我不知道其他方法如何实现

但这就像是可观察到的东西,对吧?不,可观察到的东西不喜欢 在可观察的范围内

this.currentPage$
.订阅(
(页码:编号=1)=>{
//可以通过以下方式使用“忙”:
this.busy=this.getAlerts(第页)
.烟斗(
map((响应:HttpResponse)=>
答复.结果
)
)
.订阅(
(警报:警报[])=>{…},
(错误:any)=>{…}
);
},
(错误:any)=>{…}
);

如果您希望在管道流中激发副作用,则点击
操作符是转到:

this.currentPage$.pipe(
  exhaustMap((page: number = 1) => { 
    if(page) {
      const obs$ = this.getAlerts(page).pipe(share());
      this.busy = obs$.subscribe();
      return obs$;
    } else
      return empty();
  }),
  map((response: HttpResponse<Alert[]>) => response.results),
).subscribe(
  (alerts: Alert[]) => { ... },
  (error: any) => { ... }
);
此.currentPage$.pipe(
排气图((页码=1)=>{
若有(第页){
const obs$=this.getAlerts(第页).pipe(共享());
this.busy=obs$.subscribe();
返回obs$;
}否则
返回空();
}),
map((响应:HttpResponse)=>response.results),
).订阅(
(警报:警报[])=>{…},
(错误:any)=>{…}
);

这么多空格,伙计,我甚至都看不懂,哈哈。谢谢,这似乎是可行的,但如果你在稍微不同的示例中这样做,
defaustmap
参数是假的,
为空()
被返回,请求将不会发生,第二次
点击
将永远不会被调用。此外,如果请求失败,我需要在订阅的最后一个块中重复取消订阅,以保证取消订阅被调用。如果可能的话,似乎最好将此绑定到Http请求。@mtpultz要发送http请求,请使用http拦截器,但在拦截器内部,您必须考虑下一步、错误和完成事件。这也意味着要处理队列中的多个请求,并设置一个全局服务来共享繁忙的订阅。@mtpultz Hmmm…好的,这应该可以做到。现在它直接绑定到http observ启用并使用
share()
可防止多个订阅导致多个HTTP请求。
this.currentPage$.pipe(
  exhaustMap((page: number = 1) => { 
    if(page) {
      const obs$ = this.getAlerts(page).pipe(share());
      this.busy = obs$.subscribe();
      return obs$;
    } else
      return empty();
  }),
  map((response: HttpResponse<Alert[]>) => response.results),
).subscribe(
  (alerts: Alert[]) => { ... },
  (error: any) => { ... }
);