简体   繁体   English

订阅 angular 中的 json 文件

[英]Subscribe to a json file in angular

I am reading a variable from a JSON file, which is getting updated by a .NET application.我正在从 JSON 文件中读取一个变量,该文件正在由 .NET 应用程序更新。 In my angular component, I am taking input from this JSON file.在我的 angular 组件中,我从这个 JSON 文件中获取输入。 Can we subscribe to any changes in this input JSON file?我们可以订阅此输入 JSON 文件中的任何更改吗?

The reason why I am reading from the JSON file because the workflow is like this.我之所以从 JSON 文件中读取,是因为工作流程是这样的。 The .NET application is providing me with input, and on another server, an Angular web application is running which takes input. .NET 应用程序正在为我提供输入,而在另一台服务器上,Angular web 应用程序正在运行,它需要输入。 Through the route, I can't pass a complex object like this.通过路线,我不能像这样通过复杂的 object。

inputIDs = [
    {
      entityType: "A",
      guids: [
        "6EACCBAE83E",
        "F3354548399",
      ],
    },
    {
      entityType: "B",
      guids: [
        "4CB2FF7EC199",
        "DC60A9F4weCA",
      ],
    },
  ];

And that's why I decided to use a json file in between.这就是为什么我决定在两者之间使用 json 文件的原因。 Is this approach also correct?这种方法也正确吗?

If you want updates from this json file you will need to use "polling".如果您想从此 json 文件更新您将需要使用“轮询”。 You can read more about how to do this with rxjs here: Polling in rxjs .您可以在此处阅读有关如何使用 rxjs 执行此操作的更多信息: Polling in rxjs

This basicly will request the complete json file every X seconds/minutes.这基本上将每 X 秒/分钟请求一次完整的 json 文件。 In Angular it will look like a normal GET http call to the correct file path/url where the file is located.在 Angular 中,它看起来像一个正常的 GET http 调用文件所在的正确文件路径/url。

If you want to get the updates to the file instantaneously you will have to look into push messaging.如果您想立即获取文件的更新,则必须查看推送消息。 Basicly sending messages from the server to the client.基本上是从服务器向客户端发送消息。 This is very easy to do in Angular but will require some backend work in setting this up.这在 Angular 中很容易做到,但在设置时需要一些后端工作。

And that's why I decided to use a json file in between.这就是为什么我决定在两者之间使用 json 文件的原因。 Is this approach also correct?这种方法也正确吗?

I would never recommend it, this is not the way modern webapps communicate with servers.我永远不会推荐它,这不是现代 webapps 与服务器通信的方式。 You should define an endpoint in your backend which returns a json response to the frontend.您应该在后端定义一个端点,该端点向前端返回 json 响应。 This is because saving a file on a filesystem is quite error prone.这是因为在文件系统上保存文件很容易出错。 You have the possibility for all kinds of locking and corruption to occure.您有可能发生各种锁定和损坏。 This ofcourse depends in what kind of environment you are working and how much choice you have in modifying excisting code.这当然取决于您正在工作的环境类型以及您在修改现有代码方面有多少选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM