简体   繁体   English

返回自定义Observable时,Angular HTTP客户端拦截器不起作用

[英]Angular HTTP Client Interceptor not working when returning custom Observable

FYI This issue is related to Angular Universal 仅供参考此问题与Angular Universal有关

I am currently trying to create an HttpClient Interceptor that caches GET requests and returns the cached response as an observable. 我目前正在尝试创建一个HttpClient拦截器,该拦截器缓存GET请求并以可观察的方式返回缓存的响应。

I tried following the guide set by Angular interceptors here https://github.com/angular/angular/blob/master/aio/content/guide/http.md but I can't seem to get it working, when I return an Observable other than next.handle() the subscription just doesn't get triggered. 我试图在这里角拦截器设置为指导以下https://github.com/angular/angular/blob/master/aio/content/guide/http.md ,但我似乎无法得到它的工作,当我返回除了next.handle()以外,其他可观察到的订阅都不会被触发。

Here is a plunker demoing it not working. 这是一个矮子,演示它不起作用。 https://plnkr.co/edit/QzVpuIoj3ZFXgHm7hVW2?p=preview https://plnkr.co/edit/QzVpuIoj3ZFXgHm7hVW2?p=preview

Example interceptor: 拦截器示例:

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  // if this is cached, return the cached response.
  return Observable.of({body: 'Test'});

  return next
    .handle(customReq)
    .do((ev: HttpEvent<any>) => {
      if (ev instanceof HttpResponse) {
        // Cache this response
      }
    });
 }

Any help would be greatly appreciated. 任何帮助将不胜感激。

My goal ultimately is to get this working with NG Universal's Transfer Module. 我的最终目标是使此功能与NG Universal的传输模块一起使用。 If I fix this problem in the interceptor then I should have that all working and ready to go. 如果我在拦截器中解决了这个问题,那么我应该已经全部准备好了。

Edit: My assumption was wrong. 编辑:我的假设是错误的。 The Angular HttpClient Interceptor Caching example works absolutely fine, it must be Angular Universal that is failing for me. Angular HttpClient Interceptor Caching示例工作得很好,对于我来说,它一定是Angular Universal。 (I updated Plunker with the working example from Angular, to prove that it's working for that case). (我用Angular的工作示例更新了Plunker,以证明它适用于这种情况)。

Here is my interceptor I use for Angular Universal. 这是我用于Angular Universal的拦截器。

constructor(private state: TransferState) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (request.method !== "GET") {
        return next.handle(request);
    }

    const STATE_KEY = makeStateKey(request.urlWithParams);
    const cachedResponse = this.state.get(STATE_KEY, null);

    if (cachedResponse) {
        return Observable.of(cachedResponse);
    }

    return next.handle(request).do(event => {
        if (event instanceof HttpResponse) {
            this.state.set(STATE_KEY, event.clone());
        }
    });
}

Maybe it's some sort of timing issue with the TransferState service, I am not sure. 我不确定,这可能是TransferState服务的某种计时问题。

I got it working. 我知道了 It makes sense but I can't believe it took me so long to realise the small fix. 这是有道理的,但我不敢相信我花了这么长时间才意识到这个小问题。 When I am returning the Observable of cachedResponse, I need to create a new HttpResponse object, since it's a class not just an interface. 当我返回Observable的cachedResponse时,我需要创建一个新的HttpResponse对象,因为它是一个类,而不仅仅是一个接口。 Thus like this: 因此像这样:

return Observable.of(new HttpResponse<any>(cachedResponse));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM