尝试build a login demo on stackblitz和IIUC时,login
的第二个参数应该收到throwError
的结果,但不是。任何想法:
login(username: string, password:string) {
this.authenticate(username, password).subscribe(
user => {
this.ostore.put(USER_KEY, user);
this.ostore.put(AUTHENTICATION_ERROR_KEY, false);
this.router.navigate(['/']);
}),
(error)=>{
console.log("Storing the Error");
error => this.ostore.put(AUTHENTICATION_ERROR_KEY, AUTHENTICATION_ERROR_MESSAGE);
}
}
private authenticate(username:string, password:string) {
// Mock Authentication Check
if (username !== 'user') {
return throwError(AUTHENTICATION_ERROR_MESSAGE);
}
return of({ name: username });
}
答案 0 :(得分:1)
接收错误的不是第二个参数,而是susbscribe的第二个回调。像这样进行“登录”:
login(username: string, password:string) {
this.authenticate(username, password).subscribe(
user => {
this.ostore.put(USER_KEY, user);
this.ostore.put(AUTHENTICATION_ERROR_KEY, false);
this.router.navigate(['/']);
},
error => {
console.log("Storing the Error");
error => this.ostore.put(AUTHENTICATION_ERROR_KEY, AUTHENTICATION_ERROR_MESSAGE);
})
}