Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何从Utils方法订阅主题_Angular_Typescript_Observable_Subscription_Retrywhen - Fatal编程技术网

Angular 如何从Utils方法订阅主题

Angular 如何从Utils方法订阅主题,angular,typescript,observable,subscription,retrywhen,Angular,Typescript,Observable,Subscription,Retrywhen,我已经从中实现了第二个示例-它工作完美,如果看到503,将重新启动该方法 现在,我的目标是在第一次重试时在前端显示一条警告消息。但是,该方法位于utils类中,与我要在其中设置显示警告消息的标志的组件不同 RxJS utils类 export const genericRetryStrategy = ( { maxRetryAttempts = 4, scalingDuration = 1000, excludedStatusCodes = [], retry

我已经从中实现了第二个示例-它工作完美,如果看到503,将重新启动该方法

现在,我的目标是在第一次重试时在前端显示一条警告消息。但是,该方法位于utils类中,与我要在其中设置显示警告消息的标志的组件不同

RxJS utils类

export const genericRetryStrategy = (
  {
    maxRetryAttempts = 4,
    scalingDuration = 1000,
    excludedStatusCodes = [],
    retry = new Subject<any>(),
  }: {
    maxRetryAttempts?: number;
    scalingDuration?: number;
    excludedStatusCodes?: number[];
    retry?: any;
  } = {},
) => (attempts: Observable<any>) => {
  return attempts.pipe(
    switchMap((error, i) => {
      const retryAttempt = i + 1;
      // if maximum number of retries have been met
      // or response is a status code we don't wish to retry, throw error
      if (retryAttempt > maxRetryAttempts || excludedStatusCodes.find(e => e === error.status)) {
        return _throw(error);
      }
      // retry after 1s, 2s, 6s and 8s
      if (retryAttempt === 3 || retryAttempt === 4) {
        scalingDuration = 2000;
      }
      console.log(`Attempt ${retryAttempt}: retrying in ${retryAttempt * scalingDuration}ms`);
      return timer(retryAttempt * scalingDuration);
    }),
    finalize(() => console.log('Abandoning..')),
  );
};
public submit() {
.
.
.

this.someService // The service contains return methods
   .create(data)
   .pipe(
     retryWhen(genericRetryStrategy({
       retry: true,
       excludedStatusCodes: [200],
     })),
     catchError(error => of(error)),
   )
   .subscribe((response: someResponse) => {
     this.someService.saveData(response.data, response.jwt);
我希望RxJS utils类能够控制何时显示以下内容

前端

 <p *ngIf="retry" class="warning">
   Retrying..
 </p>

重试。。

任何关于如何实现这一点的例子/想法都将非常好


提前谢谢

在代码段中看不到您订阅的任何地方?
subscribe()
在哪里?@penleychan他可能在使用异步pipe@MoxxiManagarm,嗯,我也想到了,但似乎不太可能,因为它在
submit()
函数中,所以他可能通过单击事件或某种方式调用它。@penleychan我包含了更多的代码片段,我希望这能有所帮助-我对这一切都还不熟悉,所以对我来说你可以通过在retryWhen中点击
操作符来实现这一点,比如