angular - 我们什么时候应该使用 Angular 服务?

标签 angular angular-services

关闭。这个问题是opinion-based .它目前不接受答案。












想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题.

3年前关闭。




Improve this question




据我所知,我们在组件间和组件内通信的情况下使用服务,其中我们隐藏了多个或复杂的数据结构。我们真的只在持久化数据结构的情况下使用服务吗?那么在哪些情况下我们不应该使用服务呢?

最佳答案

我不同意你的说法。

From what I understand, we use services in the case of inter and intra components communication where we hide multiple or complex data structures.


而不是在我们不应该使用 Angular 服务时回答?我会回答什么、为什么以及何时使用服务?
`服务`
Service 是一个有特定用途的类,在 Angular 中,我们使用服务主要是为了三个目的。
1.实现任何独立于任何组件的业务逻辑
.
示例
假设您想从 DOB 计算年龄,现在您提供年份
并且逻辑可以给你年龄你不需要 HTML View 来做到这一点
,它是组件独立的
2. 访问共享数据。
在缺乏直接连接的组件之间传递数据时,例如兄弟、孙子等,您应该使用共享服务。
您可以使用 RXJS BehaviorSubject Subject 用于跨组件通信。
使用优势BehaviorSubjectSubject用于平原上的跨组件交互 getterssetters您是否不需要手动触发获取最新数据的方法。每当数据发生更改时,所有注入(inject)服务的组件都会自动收到通知。
What is the difference between Subject and BehaviorSubject???
3.对外互动
1.使用Http访问REST Web服务
-------------------------------------------------- -------------------------------------------------- -------------------------------
为什么在 Angular 中使用服务
Angular 将组件与服务区分开来,以增加模块化和可重用性。
and It's Good Practice to Delegate complex component logic to services

From Angular Style Guide
Do limit logic in a component to only that required for the view. All other logic should be delegated to services.

Do move reusable logic to services and keep components simple and focused on their intended purpose.

Why? Logic may be reused by multiple components when placed within a service and exposed via a function.

Why? Logic in a service can more easily be isolated in a unit test, while the calling logic in the component can be easily mocked.

Why? Removes dependencies and hides implementation details from the component.

Why? Keeps the component slim, trim, and focused.


在 Angular 中使用服务还可以确保您没有违反 DRY SRP 软件开发的原则。
Providing Services
来自 Angular 文档

Should you provide a service with an @Injectable decorator, in an @NgModule, or within an @Component? The choices lead to differences in the final bundle size, service scope, and service lifetime.

When you register providers in the @Injectable decorator of the service itself, optimization tools such as those used by the CLI's production builds can perform tree shaking, which removes services that aren't used by your app. Tree shaking results in smaller bundle sizes.

Angular module providers (@NgModule.providers) are registered with the application's root injector. Angular can inject the corresponding services in any class it creates. Once created, a service instance lives for the life of the app and Angular injects this one service instance in every class that needs it.

A component's providers (@Component.providers) are registered with each component instance's own injector.

Angular can only inject the corresponding services in that component instance or one of its descendant component instances. Angular cannot inject the same service instance anywhere else.

Note that a component-provided service may have a limited lifetime. Each new instance of the component gets its own instance of the service and, when the component instance is destroyed, so is that service instance


TLDR
*如果我们希望全局共享依赖项的实例并在整个应用程序中共享 `state`,我们在 `NgModule` 上配置它。
如果我们希望在组件的每个实例及其子组件之间共享一个单独的依赖项实例,我们可以在组件的 `providers` 属性上配置它。*
要获得清晰的图片通过 Angular's Hierarchical Dependency Injection system
好吧,建议始终使用根 AppModule 注册应用程序范围的服务,这使服务成为单例(只要我们的应用程序存在,它就会存在),但这完全取决于用例。
如果服务的唯一目的是在兄弟组件之间共享数据并提供一些帮助程序的方法。 向组件提供者注册并使其成为非单例
服务。

好处是当 Angular 销毁组件时,Angular 也会销毁服务并释放它占用的内存。 @ Credit
FAQ

关于angular - 我们什么时候应该使用 Angular 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625913/

相关文章:

html - 具有可扩展行的 Angular Material 表,在更改选项卡时自动扩展以 stackBlitz 示例

angular - 检查用户是否仍在 angularfire2 中通过身份验证

angular - karma 无法识别 Angular Directive(指令)

javascript - angularjs函数的调用顺序是什么(config/run/controller)?

javascript - 观察 Angular 2 中本地存储的变化

html - 如何使用 Angular 2 中的按钮触发 ngModelChange

javascript - angular4 文档和窗口未定义

angularjs - Angular 一次向多个模块添加服务

javascript - 在 Angular JS 中根据 Controller 逻辑创建服务

angular - 将数据从一个组件传递给服务,然后再传递给另一个组件