【发布时间】:2019-10-07 09:46:35
【问题描述】:
我在 Angular 7 中有以下服务:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class UserService {
private currentUserSubject: BehaviorSubject<any>;
constructor(
private http: HttpClient
) {
this.currentUserSubject = new BehaviorSubject(null);
}
public get currentUserValue(): any {
return this.currentUserSubject.value;
}
login(entity: any, inputUsername: any, inputPassword: any): any {
const httpOptions = {
headers: new HttpHeaders({
username: inputUsername,
password: inputPassword
})
};
return this.http.get(entity.url, httpOptions)
.pipe(map((user: Response) => {
console.log(user);
console.log(user.status);
if (user) {
this.currentUserSubject.next(user);
}
return user;
}));
}
}
我想获取响应状态(200、401 等)。如果我尝试订阅诸如 .pipe(map(.....)).subscribe(...) 之类的订阅,则会收到错误消息,即订阅不是函数。
另外,我在 pipe(map()) 中只得到 200 个状态响应。我在这里没有收到其他状态代码的响应。
我想根据收到的状态更新 BehaviorSubject。
如何获取服务中的所有响应及其状态?
我去过Angular 6 Get response headers with httpclient issue,但这不适用于这里。
我在 http 标头中添加了 observe: 'response',但没有任何区别。
const httpOptions = {
headers: new HttpHeaders({
username: inputUsername,
password: inputPassword,
observe: 'response',
}),
};
【问题讨论】:
-
所以您使用的是
{observe: 'response'},但它不起作用? -
@TonyNgo 如何在我的 httpOptions 中添加 {observe: 'response'}?
-
@TonyNgo {observe: 'response'} 对我不起作用
标签: angular angular-httpclient