如何在 Angular 5 中使用返回的 value/s?

How to use returned value/s in Angular 5?

我目前有以下服务:

@Injectable()
export class UserServiceService {
  userEmail: string;
  userPassword: string;

  constructor( private http: HttpClient ) { }

  login( userEmail, userPassword ){
    let body = { "email": userEmail, "password": userPassword};

    this.http.post('/customer/service/logging', body, httpOptions).subscribe(
      data => {
        console.log( data );
      },
      error => {
        console.error("There Is Something Wrong\nPlease Try Again Later...");
      }
    );

  }
}

在运行时post,如果成功,returns以下对象:

{message: "Login Successful", status: "success"}

我想做的是获取关键状态并将其用于路由(如果 successful), 然后关键消息提醒用户 his/her login 成功/失败。

如何获取这些键和值并使用它们,如上所述?

你应该先把它转换成json

this.http.post('/customer/service/logging', body, httpOptions)
.subscribe(
  data => {
    let res = data.json();
    console.log( res.status);

  },
  error => {
    console.error("There Is Something Wrong\nPlease Try Again Later...");
  }
);
this.http.post('/customer/service/logging', body, httpOptions).subscribe(
  data => {
    if(data.status === 'success'){
        alert(data.message); //or do whatever you want 
     }else {
     alert('login fails'); // or alert(data.message) with the error message from the server
    }
  },
  error => {
    console.error("There Is Something Wrong\nPlease Try Again Later...");
  }
);