在我的 Angular 8 應用程序中,我有一個基本的緩存攔截器:export class CacheInterceptor implements HttpInterceptor { constructor(private cache: CacheService) {} public intercept( req: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> { if (req.method !== 'GET') { return next.handle(req); } const cachedResponse = this.cache.get(req); if (cachedResponse) { console.log(cachedResponse); return of(cachedResponse); } return next.handle(req).pipe( filter(event => event instanceof HttpResponse), map((response: HttpResponse<any>) => { this.cache.addToCache(req, response); return response; }) ); }}我還有一個從外部 API 檢索數據的服務: public getCases(options: ModuleArguments): Observable<CaseResponse> { return this.http .get<CaseResponse>(this.URL_BASE, { params: options as HttpParams }) .pipe(map(this.cleanData, this)); }'cleanData' 方法只是循環接收到的數據并修改一些值以使它們更人性化(例如將'support_request' 變為'Support Request')。似乎正在發生的事情是在服務中“清理”數據后,CacheInterceptor 將響應添加到緩存中。因此,當再次發出相同的請求并從緩存中接收到時,服務正在嘗試清理已被清理的數據。如何確保 HTTP 響應在被服務修改之前已被攔截并添加到緩存中?
如何確保在 HTTP 攔截器之后發生在 observable 上的操作符?
一只斗牛犬
2022-01-13 16:21:03