How do I handle errors in the Service, and Subscribe to responses in the
Component?
Previously I have setup my Services like so:
post(user: User): Observable<User> {
const options = new RequestOptions({headers: this.headers});
return this.http.post('/api/auth', JSON.stringify(user), options)
.map(response => {
this.accessToken = response.headers.get('x-access-token');
return response.json()
})
.catch(handleError);
}
Which is useful because the calling Component can then easily:
this.authService.post(user).subscribe(user => {
if (!this.authService.isLoggedIn()) {
this.alertsService.alerts.push({msg: JSON.stringify(user), type:
'warning'});
return;
}
this.alertsService.alerts.push({msg: `Logged in with
${this.authService.accessToken}`, type: 'success'});
this.router.navigate(['/contacts']);
}, error => this.alertsService.alerts.push({msg: error, type: 'danger'}));
I want to move to the new HttpClient ASAP, because of your retry features
and especially the reintroduction of interceptors from AngularJS. Then my
auth pipeline will work with less hacks!
Thanks for all suggestions
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.