/ / इंटरसेप्टर के लिए अमूर्त वर्ग के आयात के लिए सही तरीका - कोणीय, टाइपस्क्रिप्ट, कोणीय 5

अमूर्त वर्ग को इंटरसेप्टर को आयात करने के लिए सही तरीका - कोणीय, टाइपस्क्रिप्ट, कोणीय 5

मेरे पास सार वर्ग को http इंटरसेप्टर में आयात करने का मुद्दा है। त्रुटि मिली: "विधि" एक फ़ंक्शन नहीं है। मैंने मॉड्यूल में वर्ग घोषित किया है:

@NgModule({
declarations: [
RootComponent
],
imports: [
BrowserModule,
Ng2Webstorage,
ApplicationRouterModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService)
],
exports: [],
providers: [
RootService,
[**HttpCache**],
{
provide: HTTP_INTERCEPTORS,
useClass: RequestInterceptor,
multi: true
}
[...]

इंटरसेप्टर के लिए आयातित वर्ग (वास्तव में मैं कैशिंग इंटरसेप्टर बनाने की कोशिश करता हूं):

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);

// First, check the cache to see if this request exists.
const cachedResponse = this.cache.get(req);

संपूर्ण सार वर्ग ऐसा दिखता है:

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;
}

जब मैं ऐप शुरू कर रहा हूं, तो त्रुटि प्राप्त करें ERROR TypeError: this.cache.get is not a function.

कृपया इसकी सहायता के लिए इससे जुड़े:

angular.io/guide/http#intercepting-all-requests-or-responses

उत्तर:

जवाब के लिए 2 № 1

तुम गलत कर रहे हो। HttpCache एक अमूर्त वर्ग है और HttpCache वर्ग के तरीकों को लागू करने की आवश्यकता है। बस एक क्लास बनाएं और इसे लागू करें। कैश अनुरोध (डेटा) जिसे आप चाहते हैं, उसमें अपना तर्क लिखें और प्राप्त कैश्ड डेटा प्राप्त करें।

उम्मीद है कि यह मदद करेगा