Catching Rejections

To catch rejections we use the subscriber's error and complete callbacks.

  1. import { Http } from '@angular/http';
  2. import { Injectable } from '@angular/core';
  3. @Injectable()
  4. export class AuthService {
  5. constructor(private http: Http) {}
  6. login(username, password) {
  7. const payload = {
  8. username: username,
  9. password: password
  10. };
  11. this.http.post(`${ BASE_URL }/auth/login`, payload)
  12. .map(response => response.json())
  13. .subscribe(
  14. authData => this.storeToken(authData.id_token),
  15. (err) => console.error(err),
  16. () => console.log('Authentication Complete')
  17. );
  18. }
  19. }

原文: https://angular-2-training-book.rangle.io/handout/http/catching-rejections/