Witam próbuję otrzymać błąd wysłany z bloku catch (usługi). w wielu komponentach muszę pokazać popup z wyświetlonym komunikatem o błędzie. proszę dać mi znać jak stworzyć ogólną metodę i zadzwonić do niej w bloku serwisowym. Tak jak teraz robię z "showErrorPage()".Najlepsza praktyka w obsłudze błędów w Angular2
import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map'
@Injectable()
export class DataService {
private reqData = {};
private url: string;
constructor(private http: Http) {
}
getResult(searchObject: {}): Observable<Response> {
// some logic
return this.http.post(<my url>, <data to be sent>)
.map((response: Response) => {
return response;
})
.catch((error: any) => {
if (error.status === 302 || error.status === "302") {
// do some thing
}
else {
return Observable.throw(new Error(error.status));
}
});
}
}
I w moim składnika Wołam go jak
import { Component,EventEmitter, Output, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
// importing DataService ';
@Component({
selector: 'o-result',
templateUrl: './o-result.component.html',
})
export class AComp implements OnInit {
constructor(
private dataService: DataService
){
}
ngOnInit() {
this.dataService.getResult(<url>, <params>)
.subscribe(
response => {
// doing logic with responce
}
,
error => {
this.showErrorPage();
}
)
}
showErrorPage(): void {
// displaying error in popup
}
}
Dzięki za pomoc !! – Vaibhav