我创建了一个自定义http扩展HttpInterceptor,如下所示:
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private router:Router,
private store: Store<fromAuth.State>){
}
intercept(req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(req).do(evt => {
console.log(evt);
if(evt instanceof HttpRequest){
console.log(evt);
}
if (evt instanceof HttpResponse) {
if(evt.status === 401){
this.router.navigateByUrl('/');
}else if(evt.status === 202){
console.log(evt);
this.store.dispatch(new auth.LoginSuccessAction(evt.body));
}else if(evt.status === 200){
console.log(evt);
}
else{
console.log('other')
}
}
});
}
}
然后在我的appModule提供程序中添加:
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
现在它工作了,我的http可以用httpClient拦截!
然后我通过extends Http:
创建了一个自定义http@Injectable()
export class AuthHttp extends Http {
....
还在appModule提供程序中添加:
{AuthHttp},
这个AuthHttp仍在使用!
但我发现我的AuthInterceptor
无法拦截AuthHttp
怎么修这个?谢谢!