Ответ 1
Http
не позволяет иметь более одной пользовательской реализации. Но как сказал @estus, команда Angular недавно добавила новую службу HttpClient (выпуск 4.3), которая поддерживает концепцию нескольких перехватчиков. Вам не нужно расширять HttpClient
, как вы это делаете со старым Http
. Вместо этого вы можете предоставить реализацию для HTTP_INTERCEPTORS
, которая может быть массивом с параметром 'multi: true'
:
import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
...
@NgModule({
...
imports: [
... ,
HttpClientModule
],
providers: [
... ,
{
provide: HTTP_INTERCEPTORS,
useClass: InterceptorOne,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: InterceptorTwo,
multi: true,
}
],
...
})
перехватчики:
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
...
@Injectable()
export class InterceptorOne implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('InterceptorOne is working');
return next.handle(req);
}
}
@Injectable()
export class InterceptorTwo implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('InterceptorTwo is working');
return next.handle(req);
}
}
Этот вызов сервера будет печатать сообщения журналов обоих перехватчиков:
import {HttpClient} from '@angular/common/http';
...
@Component({ ... })
export class SomeComponent implements OnInit {
constructor(private http: HttpClient) {}
ngOnInit(): void {
this.http.get('http://some_url').subscribe();
}
}