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

查看:29
本文介绍了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 的架构 > 服务部分显示具有数据数组的服务的代码(这是否正确?).

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.

如果我们将数据存储在服务中,我们将大量使用我们的服务来检索数据,并以这种方式操作数据(假设我们希望在前端使用这些数据),这样我们的服务就拥有一组数据,各个组件可以随时调用服务数据,获取一致的数据.

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天全站免登陆