#捕获异常
要捕获拒绝,我们使用订阅者的 error
和 complete
的回调。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26import { HttpClient } from "@angular/common/http";
import { Injectable } from '@angular/core';
()
export class AuthService {
constructor(private http: HttpClient) {}
login(username, password) {
const payload = {
username: username,
password: password
};
this.http.post(`${ BASE_URL }/auth/login`, payload)
.catch( error => {
console.error("error catched", error);
return Observable.of({description: "Error Value Emitted"});
})
.subscribe(
authData => this.storeToken(authData.id_token),
(err) => console.error(err),
() => console.log('Authentication Complete')
);
}
}