在 Angular 中将数据与 httpResponse 正文分开

Separating data from a httpResponse body in Angular

我对 Angular 很陌生。我需要从组件中的响应中分离出 'jwt' 令牌,以将其存储在本地存储中。由于我也需要 response.status,因此无法获得文本形式的响应。有人可以告诉我如何将它分开吗?我在这里尝试了很多解释,但似乎没有什么适合我的。

我的登录方式;

checkLogin(){
this.service.generateToken(this.credentials).subscribe(
  (response) => {
    if(response.status == 200){
      this.message = "Login Successfull"
      localStorage.setItem("token",//save the token);
      this.router.navigate(['']);
    }
},
(error) => {
    this.message = "Invalid Username or Password";
});
}

http 服务

public generateToken(request):Observable<any>{
return this.http.post<any>("http://localhost:8080/authenticate", request,{observe:'response'});
}

我的回复格式:

Response Format

如果你能够获得 response.status 值那么 response.body.jwt 也可以访问,你可以尝试这种方式吗

(response) => {
    if(response.status == 200){
      this.message = "Login Successfull"
      localStorage.setItem("token",response.body.jwt);
      this.router.navigate(['']);
    }