1

因此,基本上在我的 Angular 项目中,我试图通过以下代码行使用Cache API缓存 HttpClient 的请求和响应:

from(caches.open(this.getCacheName(this.chatAttachmentsCache)))
  .pipe(map(cache => {
    cache.put(request, response);
  }));

request 参数是请求的 URL, response 参数是请求的响应。

我将从成功的 HttpClient GET 请求中获得的 HttpResponse 对象作为响应传递给 cache.put 调用。但是,我在文本编辑器中不断收到此错误。

Argument of type 'HttpResponse<Blob>' is not assignable to parameter of type 'Response'.
  Type 'HttpResponse<Blob>' is missing the following properties from type 'Response': redirected, trailer, bodyUsed, arrayBuffer, and 4 more

我从以下代码行获取 HttpResponse 对象:

    return this.httpClient.get(url, { responseType: "blob", observe: "response" });

我真的不知道如何解决这个问题。这是否意味着我不能在 Angular 中将缓存 API 与 HttpClient 一起使用?还是有一些解决方法来处理这样的问题?

提前致谢。

4

1 回答 1

0

I managed to resolve this issue.

For anyone experiencing the same issue. I created a new Response object through the constructor and passed the body, status and status text from the HttpResponse to it as per the documentation here. Here is an example:

const responseAsResponseType = new Response(response.body, {
  "status": response.status,
  "statusText": response.statusText,
});
于 2021-05-03T13:49:06.287 回答