【问题标题】:How can get real time data from an HttpClient request in Angular 6如何从 Angular 6 中的 HttpClient 请求中获取实时数据
【发布时间】:2018-09-27 09:40:07
【问题描述】:

我的 Angular 6 应用程序遇到了这个问题。我需要始终从 HttpClient 请求中获取最新数据。

我正在调用服务以从服务器文件系统上的 json 文件中检索数据:

this.testDataService.retrieveConfig().subscribe(
    response => {
      console.log("test -> ", response);
      this.testDataService.initConfig(response);
      this.config = this.testDataService.getTest();
    },
    error => {
      console.log("Error",error);
    }
  );

这似乎很好,但问题是,在我构建的应用程序中,每次我调用 get 方法时,(它在我的 console.log 中验证为订阅箭头函数)即使我改变,响应也不会改变我的 json 文件信息。这不会发生在我当地的 angular-cli 环境中。清除浏览器缓存不是更改信息的解决方案。不起作用重启 Web 服务器 (apache) 或使用 CTRL+F5 刷新页面。

我还不明白问题是否出在 Angular 调用或 Web 服务器配置上。

谢谢。

【问题讨论】:

  • 这可能是一个缓存问题..你能发布retrieveConfig()函数的代码吗?尝试向您请求的资源添加时间戳 qyerystring。
  • 你可以在 observable 上使用 valuechanges()。如果retrieveConfig 的数据发生更改,这将创建一个websocket 并进行轮询。如果会执行响应函数。
  • 一种快速的方法是将当前时间戳作为参数添加到您的查询中:path/to/api?t=1538041780
  • 将时间戳信息添加到uri中,问题就解决了。谢谢大家!

标签: angular apache caching httpclient angular6


【解决方案1】:

解决方案(解决方法)是 Ritesh 之前和 Ploppy 之后如何将时间戳信息添加到 URI 中以使每个请求“唯一”。

谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2018-12-16
    • 1970-01-01
    相关资源
    最近更新 更多