【问题标题】:rxjs simple do http get does nothingrxjs simple do http get 什么都不做
【发布时间】:2016-10-10 15:44:19
【问题描述】:

我有代码:

export class Test {

constructor(private http: Http) {}

logResponse(): Observable<any> {
  return this.http.get('http://google.com')
    .do(data => console.log("All: " +  JSON.stringify(data)));
  }
}

但是,遗憾的是,没有任何东西打印到控制台上。我刚从 Rxjs 开始,很抱歉这个蹩脚的问题。为什么没有记录?我拥有所需的所有 http 和导入,没有任何例外,只是没有打印任何内容。我在按下按钮时触发它。

【问题讨论】:

  • 看来你需要一个承诺

标签: javascript http typescript angular rxjs


【解决方案1】:

您必须先添加subscribe 才能激活它。这个 observable 很冷 (hot vs cold observables)。

所以在你的情况下:

logResponse(): Observable<any> {
  return this.http.get('http://google.com')
    .do(data => console.log("All: " +  JSON.stringify(data)))
    .subscribe(data => console.log("All from subscribe: " +  JSON.stringify(data)));
  }
}

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2019-03-08
    • 2016-12-24
    • 2017-04-18
    • 2011-08-03
    相关资源
    最近更新 更多