gpt4 book ai didi

angular - 错误 : No provider for HttpHandler in angular2

转载 作者:太空狗 更新时间:2023-10-29 16:47:42 47 4
gpt4 key购买 nike

我正在尝试通过拦截器实现 HttpCache。以下是缓存拦截器.service.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';

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

我从 test.service.ts 调用

import { Injectable } from '@angular/core';
import { Headers, Http, Response} from '@angular/http';
import { HttpClient} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { ReplaySubject } from 'rxjs/ReplaySubject';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

import { BehaviorSubject } from 'rxjs/BehaviorSubject';

import { APIService } from './api.service';
import { CachingInterceptor } from './caching-interceptor.service';
import { ConfigurationService } from './configuration.service';
import { AuthenticationStatus, IAuthenticationStatus } from '../models';
import { User } from '../models/user.model';

@Injectable()
export class PlatformService extends APIService {



constructor(private http: Http, public httpClient: HttpClient, private configuration: ConfigurationService,
public cachingInterceptor: CachingInterceptor) {
super();


}

getUserById(id: string) {
console.log(this.requestOptions);
return this.httpClient.get(this._getAPIUrl('user/' + id),this.requestOptions).
subscribe(res => res);
}
get requestOptions(): RequestOptions {
const tokenObj = window.localStorage.getItem('TOKEN');
const token = JSON.parse(tokenObj);
const headers = this.headers;
headers.append('Authorization', 'Bearer ' + token.token);
headers.append('Access-Control-Allow-Origin', '*');
return new RequestOptions({ headers: headers });
}


}

后面是模块文件

import { CommonModule } from '@angular/common';
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
import { FormsModule } from '@angular/forms';

import { ModuleWithProviders, NgModule } from '@angular/core';

import { PlatformService } from '../../services/platform.service';
import { CachingInterceptor } from '../../services/caching-interceptor.service';


@NgModule({
imports: [CommonModule, FormsModule],

declarations: [],

exports: [],

entryComponents: [EntryHereComponent]
})
export class StructurModule {
public static forRoot(): ModuleWithProviders {
return { ngModule: StructurModule, providers: [PlatformService,
{
provide: HTTP_INTERCEPTORS,
useExisting: CachingInterceptor,
multi: true
},HttpClient] };
}
}

我不明白,缺少什么所以它给出错误

No provider for HttpHandler.

如果我在模块文件的 provider 中添加 HttpHandler,它会开始给出 provide: HTTP_INTERCEPTORS, component 的错误。

最佳答案

HttpClient 是在 angular 4.3 中引入的,所以如果你想使用 HttpClient 你需要从 '@angular 导入 HttpClientModule/common/http'。确保在 BrowserModule 之后导入 HttpClientModule,如下所示。这official docso answer会给你深入的信息。

import { HttpClientModule } from '@angular/common/http';

@NgModule({
imports: [
BrowserModule,
HttpClientModule
],
...

关于angular - 错误 : No provider for HttpHandler in angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46000748/

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