将抽象 class 导入拦截器的正确方法
Correct way for importing abstract class to interceptor
我在将摘要 class 导入 http 拦截器时遇到问题。收到错误:'method' 不是函数。我在模块中声明了 class:
@NgModule({
declarations: [
RootComponent
],
imports: [
BrowserModule,
Ng2Webstorage,
ApplicationRouterModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService)
],
exports: [],
providers: [
RootService,
[**HttpCache**],
{
provide: HTTP_INTERCEPTORS,
useClass: RequestInterceptor,
multi: true
}
[...]
也导入了class拦截器(其实我是尝试制作缓存拦截器):
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);
整个摘要 class 看起来像:
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
你做错了。 HttpCache 是一个抽象 class,您需要实现 HttpCache 的方法 class。只需创建一个 class 并实现它。在 put 方法中编写你的逻辑,以缓存你想要的请求(数据),并从 get 中获取那些缓存的数据。
希望对您有所帮助
我在将摘要 class 导入 http 拦截器时遇到问题。收到错误:'method' 不是函数。我在模块中声明了 class:
@NgModule({
declarations: [
RootComponent
],
imports: [
BrowserModule,
Ng2Webstorage,
ApplicationRouterModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService)
],
exports: [],
providers: [
RootService,
[**HttpCache**],
{
provide: HTTP_INTERCEPTORS,
useClass: RequestInterceptor,
multi: true
}
[...]
也导入了class拦截器(其实我是尝试制作缓存拦截器):
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);
整个摘要 class 看起来像:
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
你做错了。 HttpCache 是一个抽象 class,您需要实现 HttpCache 的方法 class。只需创建一个 class 并实现它。在 put 方法中编写你的逻辑,以缓存你想要的请求(数据),并从 get 中获取那些缓存的数据。
希望对您有所帮助