TypeScript Angular 2 响应订阅

TypeScript Angular 2 Responses Subscribing

本文关键字:响应 Angular TypeScript      更新时间:2023-09-26

我正在尝试从我的 api 返回信息,但我不明白如何正确使用订阅。对于数组,我从我的服务返回一个空数组,并在获取值时将值推送到其中。

我如何在应用程序组件 ts 中正确返回单个值变量。现在,如果我做一个alert(JSON.stringify(authenticated))它只会给我{"_isUnsubscribed":false,"_subscriptions":[{"isUnsubscribed":false}]}

应用程序组件 TS

checkAuthentication(){
  var authenticated = this._authService.getAuthenication();
}

认证服务

 getAuthenication() {
      return this._http.get('auth.php').subscribe(res => {
        if (res.json().authenticated === true){
          return true;
        }
        return false;
      });
  }

身份验证.php

<?
session_start();
$authenticated = ( isset($_SESSION["authenticated"]) ? true : false );
$post_data = json_encode(array(authenticated => $authenticated));
echo $post_data;
?>

更改

return this._http.get('auth.php').subscribe(res => {

return this._http.get('auth.php').map(res => {

并称它为喜欢

this._authService.getAuthenication()
    .subscribe(value => this.authenticated = value);