gpt4 book ai didi

angular - 无法在 Angular 4.3 中使用 HttpInterceptor 进行缓存

转载 作者:太空狗 更新时间:2023-10-29 17:29:48 25 4
gpt4 key购买 nike

我正在尝试根据 angular 4.3 的文档使用 HttpInterceptor 实现 HttpRequest 缓存。但我收到一个错误。这是我的代码:

缓存.拦截器.ts

import { HttpRequest, HttpResponse, HttpInterceptor, HttpHandler, HttpEvent } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import 'rxjs/add/operator/do';
import 'rxjs/add/observable/of';

abstract class HttpCache {
abstract get(req: HttpRequest<any>): HttpResponse<any>|null;
abstract put(req: HttpRequest<any>, resp: HttpResponse<any>): void;
}

@Injectable()
export class CachingInterceptor implements HttpInterceptor {
constructor(private cache: HttpCache) {}

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){
return Observable.of(cachedResponse);
}

return next.handle(req).do(event => {
if(event instanceof HttpResponse){
this.cache.put(req, event);
}
})
}
}

这里 CachingInterceptor 用作 http 请求/响应的拦截器。我创建了模块 a,如下所示:

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';

import { AppComponent } from './app.component/app.component';
import { HomePage } from './pages/home.page/home.page';
import { ProductsPage } from './pages/products.page/products.page';
import { AboutUsPage } from './pages/about-us.page/about-us.page';
import { UsersPage } from './pages/users.page/users.page';
import { DemoPage } from './pages/demo.page/demo.page';
import { appRouter } from './app.router/app.router';
import { CachingInterceptor } from './caching.interceptor/caching.interceptor';
import { AppService } from './app.service/app.service';

@NgModule({
imports: [ BrowserModule, HttpClientModule, appRouter ],
declarations: [ AppComponent, HomePage, ProductsPage, DemoPage, AboutUsPage, UsersPage ],
providers: [ {
provide: HTTP_INTERCEPTORS,
useClass: CachingInterceptor,
multi: true
}, AppService ],
bootstrap: [ AppComponent ]
})
export class AppModule {}

Token 也在模块的 providers[] 中提供。这是根据 Angular 4.3 的文档。但我仍然收到如下错误:

错误

ERROR Error: Uncaught (in promise): Error: No provider for HttpCache!
Error: No provider for HttpCache!
at injectionError (reflective_errors.ts:71)

我有两个问题:

  1. HttpCache是一个抽象类,那为什么像服务一样注入(inject)呢?
  2. 虽然我是按照官方文档实现的,但为什么会出现该错误?

最佳答案

如果您正在寻找一个 super 简单的缓存所有事物的实现:

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs/Observable";

@Injectable()
export class CacheInterceptor implements HttpInterceptor {
private cache: { [name: string]: AsyncSubject<HttpEvent<any>> } = {};

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.method !== "GET") {
return next.handle(request);
}
const cachedResponse = this.cache[request.urlWithParams] || null;
if (cachedResponse) {
return cachedResponse.delay(0);
}
const subject = this.cache[request.urlWithParams] = new AsyncSubject<HttpEvent<any>>();
next.handle(request).do(event => {
if (event instanceof HttpResponse) {
subject.next(event);
subject.complete();
}
}).subscribe(); // must subscribe to actually kick off request!
return subject;
}

请注意,这已从原始方法更新。原始的 intercept 方法有一个错误 - 如果在第一次返回之前尝试对相同 url 的多个请求,多个请求仍然会到达服务器。

此解决方案只允许将一个请求传递到服务器。

原始解决方案如下,供后人引用。 (不推荐。)

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.method !== "GET") {
return next.handle(request);
}
const cachedResponse = this.cache[request.urlWithParams] || null;
if (cachedResponse) {
return Observable.of(cachedResponse);
}

return next.handle(request).do(event => {
if (event instanceof HttpResponse) {
this.cache[request.urlWithParams] = event;
}
});
}

关于angular - 无法在 Angular 4.3 中使用 HttpInterceptor 进行缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45480785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com