【问题标题】:How to get streams of data in angular如何以角度获取数据流
【发布时间】:2022-11-07 17:37:54
【问题描述】:

试图在我的控制台上显示数据流,但可能不知道正确的工具。如果我尝试从 curl 获取数据,它工作正常,看起来像这样:

C:\Users\user>curl localhost:8080/slow/5
5
4
3
2
1
0

C:\Users\user>

首先我尝试使用标准的http get请求,

constructor(
    private http: HttpClient,
    private ngzone: NgZone
) {
  this.http.get('/slow/5').subscribe(x => console.log(x))
}

这是我在 5 秒后得到的错误:

error: Object { error: SyntaxError, text: "5\n4\n3\n2\n1\n0\n" }

尝试使用 EventSource 方法,但这甚至无法连接它。 代理工作正常。 有任何想法吗?首先十分感谢。

【问题讨论】:

    标签: angular http rxjs


    【解决方案1】:

    请检查这个 Stackblitz 示例,其中使用来自 RxJs 的 of 运算符设置了一个虚拟数据流。

    您可以在app.component.ts 中查看该数据流是如何被订阅的,以便在控制台中打印出它的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 2021-03-02
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 2018-11-06
      • 1970-01-01
      相关资源
      最近更新 更多