Angular 4-HTTP拦截器 [英] Angular 4 - HTTP Interceptor
问题描述
我正在尝试在Angular 4中创建HTTP拦截器,但出现了一些奇怪的错误.以下是我的错误:
I'm trying to create HTTP Interceptor in Angular 4 but I'm having some weird error. Following is my error:
Argument of type 'Observable<Response>' is not assignable to parameter of type 'Observable<Response>'.
以下是我的代码:
import { Injectable } from '@angular/core';
import { Http, ConnectionBackend, RequestOptions, RequestOptionsArgs } from '@angular/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import * as _ from 'lodash';
@Injectable()
export class HttpInterceptor extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.request(url, options)); // Here is the error
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.get(url, options)); // Here is the error
}
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.post(url, body, this.getRequestOptionArgs(options))); // Here is the error
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.put(url, body, this.getRequestOptionArgs(options))); // Here is the error
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.delete(url, options)); // Here is the error
}
getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
if (options == null) {
options = new RequestOptions();
}
if (options.headers == null) {
options.headers = new Headers(); // Here is the error
}
options.headers.append('Content-Type', 'application/json');
return options;
}
intercept(observable: Observable<Response>): Observable<Response> {
return observable.catch((err, source) => {
if (err.status == 401 && !_.endsWith(err.url, 'api/auth/login')) {
this._router.navigate(['/login']);
return Observable.empty();
} else {
return Observable.throw(err);
}
});
}
}
有人知道这里出了什么问题吗?我尝试调试了3个小时,但找不到任何线索.
Does anyone know what is going wrong here? I tried debugging for 3 hours but unable to find any clue.
我还尝试删除所有内容,并编写了如下代码:
I also tried to remove everything and written a code like this:
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return super.request(url, options);
}
但是仍然会出现相同的错误:
But still it's giving same error:
Argument of type 'string | Request' is not assignable to parameter of type 'string | Request'. Type 'Request' is not assignable to type 'string | Request'.
推荐答案
Http interceptor is already implemented in Angular 4.3.4 and is described in the documentation.
您需要实现HttpInterceptor
接口的intercept
方法,对请求进行某些操作,然后调用next.handle(req)
方法.
You need to implement the intercept
method of HttpInterceptor
interface, do something with the request, and call the next.handle(req)
method.
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
@Injectable()
export class NoopInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const changedReq = req.clone({headers: req.headers.set('My-Header', 'MyHeaderValue')});
return next.handle(changedReq);
}
}
还必须在应用程序的提供程序部分中注册拦截器
It's also necessary to register interceptor in app's providers section
import {NgModule} from '@angular/core';
import {HTTP_INTERCEPTORS} from '@angular/common/http';
@NgModule({
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: NoopInterceptor,
multi: true,
}],
})
export class AppModule {}
这篇关于Angular 4-HTTP拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!