[英]Why do we need services when we have components?
I am learning Angular and I am struggling to understand the application of Services. 我正在学习Angular,并且正在努力了解Services的应用程序。 I understand services are for reusability but that can be acheived by Components, too. 我知道服务是为了可重用性,但是组件也可以实现。 Also, we can make components public and share also. 另外,我们可以公开组件并共享。 So why exactly do we need services? 那么,为什么我们需要服务呢?
Services don't have a view associated with them (no html and css). 服务没有与之关联的视图(没有html和CSS)。 Less overhead. 更少的开销。 Also, Services can be reused anywhere without needing to create a new instance of the service. 同样,可以在任何地方重用服务,而无需创建服务的新实例。 This is invaluable if you want to share data or functions globally (or across many different components/contexts). 如果要全局(或在许多不同的组件/上下文中)共享数据或功能,这将是无价的。 With components, you -can- share the same instance of a component with other components in your app using dependency injection, but you can only share that instance with components that are descendants of the component you want to share. 使用组件,您可以使用依赖注入将组件的同一实例与应用程序中的其他组件共享,但是您只能与属于您要共享的组件后代的组件共享该实例。 This is pretty limiting. 这是相当有限的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.