0

我正在调用的一个 BACKEND API 的获取请求已超过 2 分钟。我正在尝试使用 Angular Http 拦截器来处理这个"timeout": 360000问题。当我在 proxy.config 中添加时,我的问题在一定程度上解决了。但有时它仍然会失败。这是我的拦截器类。

export const DEFAULT_TIMEOUT = new InjectionToken<number>('defaultTimeout');
@Injectable()
export class DashboardInterceptor implements HttpInterceptor {
  constructor(@Inject(DEFAULT_TIMEOUT) protected defaultTimeout: number) {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const timeoutValue = req.headers.get('timeout') || this.defaultTimeout;
    const timeoutValueNumeric = Number(timeoutValue);

    return next.handle(req).pipe(timeout(timeoutValueNumeric));
  }

}

由于它失败了,我试图在通话中发送标头。但它不允许。

getInfo(headers: new HttpHeaders({ timeout: `${360000}` }), params: any[]) {
    return this.apiService.get(environment.rm_url + 'rm-analytics-api/dashboard/txn-info', headers, params).pipe(
      timeout(360000),
      catchError(error => {
          console.log('Response error!');
          return of({ error});
        }
      ));
  }

我将此添加到dashboard.module.ts

providers:[ [DashboardServiceHandler, [{ provide: HTTP_INTERCEPTORS, useClass: DashboardInterceptor, multi: true }],
    [{ provide: DEFAULT_TIMEOUT, useValue: 360000 }]]

我也将它添加到 proxy.config 中。

"secure": false,
    "timeout": 360000,

我该如何解决这个问题?

4

2 回答 2

0

当我将它添加到 app.module.ts 文件时它起作用了。并且能够在响应到来之前保持请求而不会失败。

providers: [
    [{ provide: HTTP_INTERCEPTORS, useClass: DashboardInterceptor, multi: true }],
    [{ provide: DEFAULT_TIMEOUT, useValue: 360000 }]
  ],

而且我不想 在标题中添加new HttpHeaders({ timeout:${360000} 。})

于 2020-05-21T11:05:08.130 回答
0

HttpInterceptor 是处理它的错误位置。尝试改用 Ngrx Effects

于 2020-05-21T06:50:52.957 回答