• 捕获异常

    捕获异常

    要捕获拒绝,我们使用订阅者的 errorcomplete的回调。

    1. import { HttpClient } from "@angular/common/http";
    2. import { Injectable } from '@angular/core';
    3. @Injectable()
    4. export class AuthService {
    5. constructor(private http: HttpClient) {}
    6. login(username, password) {
    7. const payload = {
    8. username: username,
    9. password: password
    10. };
    11. this.http.post(`${ BASE_URL }/auth/login`, payload)
    12. .catch( error => {
    13. console.error("error catched", error);
    14. return Observable.of({description: "Error Value Emitted"});
    15. })
    16. .subscribe(
    17. authData => this.storeToken(authData.id_token),
    18. (err) => console.error(err),
    19. () => console.log('Authentication Complete')
    20. );
    21. }
    22. }