Angular v4:我们是否将数据存储在服务或组件中,或两者中? [英] Angular v4: Do we store data in a Service or the Component or both?

查看:84
本文介绍了Angular v4:我们是否将数据存储在服务或组件中,或两者中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular v4:我们是否将数据存储在服务或组件中,还是两者都存储?

Angular v4: Do we store data in a Service or the Component or both?

在回顾了很多教程并阅读了Angular的文档之后,我仍然不清楚这个主题.

After reviewing quite a few tutorials, as well as reading the documentation of Angular, I'm still not clear on this subject.

https://angular.io/tutorial/toh-pt2 Angular的教程清楚地显示了数据存储在组件中.

https://angular.io/tutorial/toh-pt2 Angular's tutorial clearly shows data stored in the Component.

https://angular.io/guide/architecture#services Angular的Architecture> Services部分显示带有服务的代码的数据数组(对吗?).

https://angular.io/guide/architecture#services Angular's Architecture > Services section shows code with the service having an array of data (is this proper?).

如果我们将数据存储在组件中,我们会大量使用@Input和@Output在子组件之间移动数据(假设我们希望在前端使用此数据),但是当我们使用路由,我们需要从路由器出口加载的新组件来对我们的服务进行新的调用,以承诺对服务器进行API调用来保存数据.在这种情况下,可能会有2个组件保存相同的数据-但是它们可能不匹配.

If we store data in Components, we would heavily used @Input and @Output to move data between child components (assuming we want this data in the front end), however this poses a problem when we use routing, we would need our new Component which loaded from the router-outlet to make a new call to our service for a promise to make the API call to our server to hold data. Possibly in this case we would have 2 components holding the same data - however they may not match.

如果我们将数据存储在服务中,则我们将大量使用我们的服务来检索数据,并以这种方式处理数据(假设我们希望在前端使用此数据),这样我们的服务将拥有1套数据,每个组件都可以随时调用服务数据以获得一致的数据.

If we store data in a Service, we would heavily use our Services to retrieve data, and manipulate data (assuming we want this data in the front end) this way our service holds 1 set of data, and each component may call on the service data at any time to get consistent data.

-

正确的数据存储方式是什么? 不建议其中之一吗?

What is the proper way of storing data? Is one of the other not advised?

推荐答案

通常来说,如果许多组件使用相同的数据,则希望将数据存储在服务中.这样,就可以非常轻松地从应用程序的所有不同部分访问相同的数据.如果一个组件修改了服务中的数据,则将对所有使用该数据的组件进行修改,这通常会很有帮助.但是,有时如果您仅需要将数据从一个组件发送到另一个组件(其中一个组件是另一个组件的父组件),则没有必要.在这种情况下,建议使用输入/输出.

Generally speaking, you want to store data in a service if a lot of components use the same data. That way, it makes it extremely easy to access the same data from all different parts of your app. If one component modifies the data in the service, it will be modified for all the components that use the data which is usually very helpful. However, sometimes it is unnecessary if you only need to send data from one component to another, where one is a parent of the other. In this scenario, using input/output would be advised.

如果您不需要在各个组件之间发送特定的数据,那么将数据存储在一个组件中是完全可以接受的!请记住,除非使用输入/输出,否则其他组件将无法访问它.

If you don't need to send the specific data between various components, then storing the data in a component is perfectly acceptable! Keep in mind that it will not be accessible from other components unless you use input/output.

这篇关于Angular v4:我们是否将数据存储在服务或组件中,或两者中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆