Я бы расширить Http
класс, как показано ниже, и добавить сервис, содержащий наблюдаемым/при условии уведомления, когда запрос HTTP выполняется
export class CustomHttp extends Http {
constructor(backend: ConnectionBackend,
defaultOptions: RequestOptions,
private _notifierService:NotifierService) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
this._notifierService.notifyBeforeRequest();
return super.request(url, options);
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
this._notifierService.notifyBeforeRequest();
return super.get(url, options);
}
(...)
}
зарегистрировать его, как описано ниже:
bootstrap(AppComponent, [HTTP_PROVIDERS,
{
provide: Http,
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, notifyService:NotifierService) => {
return new CustomHttp(backend, defaultOptions, notifyService);
},
deps: [XHRBackend, RequestOptions, NotifierService]
}
]);
Сервисный инструмент может быть примерно таким:
export class NotifierService {
notifier:Subject<any> = new Subject();
notifyBeforeRequest() {
notifier.next();
}
}
Вы можете получить уведомление таким образом:
@Component({
(...)
})
export class SomeComponent {
constructor(private notifyService:NotifierService) {
this.notifyService.notifier.subscribe(() => {
// do something
});
}
}