In my data service , I am trying to display error using toastr, on an exception.
To do that I am creating a class, loading the API , on success return data , else handle the exception.
export class DataService {
private productUrl = "api/Products11";
constructor(public http: HttpClient, private toastr: ToastrService) { }
public products: Product[] = [];
loadProducts(): Observable<boolean> {
return this.http.get(this.productUrl)
.pipe(
map((data: any[]) => {
this.products = data;
return true;
}), catchError(this.handleError));
}
private handleError(err: HttpErrorResponse) {
//this.toastr is undefined here
this.toastr.success(errorMessage);
}
}
But, When I set a break point on handleError , I am getting this.toastr
as undefined. Same, if I am trying to access this.productUrl
.
Everything works fine on loadProducts method.