【问题标题】:TypeScript Angular 2 Responses SubscribingTypeScript Angular 2 响应订阅
【发布时间】:2016-02-20 14:20:34
【问题描述】:

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

如何正确返回 app-component 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;
      });
  }

auth.php

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

【问题讨论】:

    标签: javascript typescript angular


    【解决方案1】:

    改变

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

    到

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

    然后这样称呼它

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

    【讨论】:

    • 我得到属性 'subscribe' 在类型 'Subscription' 上不存在。是订阅 angular 还是 javascript,有相关文档吗?
    • 抱歉,忘记更改
    • 对不起,再次。应该是value :-/
    • 我不知道哪些文档适用于大多数关于 Rx 的文档,尤其是我猜的 Rxjs。我只使用飞镖。 Rxjs 类似于 Dart 中的流,我在 SO 上看到了它是如何用于几个问题和答案的。
    • 你应该得到每个人的双重支持。感谢您指出这一点。
    猜你喜欢
    • 1970-01-01
    • 2016-10-09
    • 2019-05-12
    • 2018-04-24
    • 2017-02-02
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    相关资源
    最近更新 更多