1.我按照angular官方文档实现了httpclient发出http请求。文档链接
2.我尝试按文档在项目中实现拦截器,这是我的代码:
Universal.Interceptor.ts
import { Injectable, Inject,Injector, Optional } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/observable';
import { StorageService } from '../../shared/storage.service';
import { enable, destroy } from 'splash-screen';
import 'rxjs/add/operator/do';
@Injectable()
export class UniversalInterceptor implements HttpInterceptor {
private _StorageService: StorageService;
constructor(private injector: Injector) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(req)
return next.handle(req);
}
}```
app.module.ts
...
**import { HTTP_INTERCEPTORS } from '@angular/common/http';**
**import { UniversalInterceptor } from './core/Interceptor/Universal.Interceptor';**
@NgModule({
declarations: [
AppComponent
],
...
**providers: [{ provide: HTTP_INTERCEPTORS, useClass: UniversalInterceptor, multi: true }],**
bootstrap: [AppComponent]
})
}
项目的http请求还是正常的,但拦截器的console.log没有打印,项目也没有报错,是否引入少了内容?Universal.Interceptor.ts文件是我手动创建的,是否跟这个有关系?有没有使用这个拦截器的
你好,升级到4.3以后,新多出个HttpClient模块,如果要使用拦截器,需要,将老的http改为,HttpClient.
相关代码: