I encountered an issue while trying to import an abstract class into an HTTP interceptor. The error message I received was: 'method' is not a function. I have declared the class within the module as follows:
@NgModule({
declarations: [
RootComponent
],
imports: [
BrowserModule,
Ng2Webstorage,
ApplicationRouterModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService)
],
exports: [],
providers: [
RootService,
[**HttpCache**],
{
provide: HTTP_INTERCEPTORS,
useClass: RequestInterceptor,
multi: true
}
[...]
I also imported the class into the interceptor in an attempt to create a caching interceptor:
import { HttpCache } from './interface/http-cache.interface';
@Injectable()
export class CachingInterceptor implements HttpInterceptor {
constructor(private cache: HttpCache) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.method !== 'GET') {
console.warn('HTTP request different form GET');
return next.handle(req);
}
console.warn('Caching Interceptor: ', this.cache);
// Checking the cache for the request.
const cachedResponse = this.cache.get(req);
The structure of the abstract class is as follows:
export abstract class HttpCache {
/**
* Returns a cached response, if any, or null if not present.
*/
abstract get(req: HttpRequest<any>): HttpResponse<any>|null;
/**
* Adds or updates the response in the cache.
*/
abstract put(req: HttpRequest<any>, resp: HttpResponse<any>): void;
}
Upon starting the app, I received the error
ERROR TypeError: this.cache.get is not a function
.
I would appreciate any help regarding this issue and it is related to:
angular.io/guide/http#intercepting-all-requests-or-responses