我们什么时候应该使用 angular 服务?
when we should use angular service?
据我了解,我们在隐藏多个或复杂数据结构的组件间和组件内通信的情况下使用服务。在持久化数据结构的情况下,我们真的只使用服务吗?那么什么情况下我们不应该使用服务呢?
您从哪里听说这是不好的做法?
使用服务通过 HTTPRequest 从 API 获取数据或在组件之间共享数据是一种很好的做法,请在此处查看我的示例:
Share methods between two components in same page Angular
您必须记住的重要一点是服务是应用程序范围的单例。 因此只将服务放入 CoreModule,并且只在 AppModule 中导入 CoreModule 一次,其他地方都不导入。 您不希望每个模块都有自己单独的实例。 (更多关于 CoreModule 和 SharedModule 的区别:)
然而,如果 SharedModule 提供服务,则确实存在发生这种情况的危险。例如,如果您将服务用于某些可重用组件,则可能需要它,例如。 in 指令实际上在 SharedModule 中。然后你必须在导入 ShareModule 时使用 forRoot()
,例如:
import { NgModule, ModuleWithProviders } from '@angular/core';
import { MyDirective } from './my.directive';
import { SomeService } from './some.service';
@NgModule({
declarations: [
MyDirective
],
exports: [
MyDirective
]
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [ SomeService ]
};
}
}
一些其他模块:
import { SharedModule } from './shared/shared.module';
@NgModule({
imports: [
SharedModule.forRoot()
]
})
export class SomeModule {}
希望对您有所帮助:)
我不同意你的说法。
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就是一个class有特定用途的,而在Angular中,我们主要为了三个目的使用服务。
1.To 实现独立于任何组件的任何业务逻辑
.
例子
假设您想从 DOB 计算年龄,现在您提供年份
和逻辑可以给你年龄你不需要 HTML 视图来做到这一点
,它是组件 Independent
2。访问共享数据。
在缺乏直接连接的组件(例如兄弟姐妹、孙子等)之间传递数据时,您应该使用共享服务。
您可以使用 RXJS BehaviorSubject
or Subject
进行跨组件通信。
与普通 getters
和 setters
相比,使用 BehaviorSubject
或 Subject
进行跨组件交互的优点是您不需要手动触发获取最新数据的方法。每当数据发生变化时,所有注入服务的组件都会自动得到通知。
3。外部互动
1.Accessing 使用 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 and 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”上。
如果我们希望在组件的每个实例及其子实例之间共享依赖项的单独实例,我们可以在组件“提供者”属性 上配置它。*
要获得清晰的图片,请通过 Angular's Hierarchical Dependency Injection system
好吧,建议始终使用根 AppModule 注册应用程序范围的服务,这使得服务成为单例(它会在我们的应用程序存在时存在),但这完全取决于用例。
如果服务的唯一目的是在同级组件之间共享数据并提供几个辅助方法。向组件提供者注册它并使其成为非单例
服务.
好处是在Angular销毁组件的同时,Angular也会销毁服务并释放其占用的内存。@ Credit
据我了解,我们在隐藏多个或复杂数据结构的组件间和组件内通信的情况下使用服务。在持久化数据结构的情况下,我们真的只使用服务吗?那么什么情况下我们不应该使用服务呢?
您从哪里听说这是不好的做法? 使用服务通过 HTTPRequest 从 API 获取数据或在组件之间共享数据是一种很好的做法,请在此处查看我的示例: Share methods between two components in same page Angular
您必须记住的重要一点是服务是应用程序范围的单例。 因此只将服务放入 CoreModule,并且只在 AppModule 中导入 CoreModule 一次,其他地方都不导入。 您不希望每个模块都有自己单独的实例。 (更多关于 CoreModule 和 SharedModule 的区别:
然而,如果 SharedModule 提供服务,则确实存在发生这种情况的危险。例如,如果您将服务用于某些可重用组件,则可能需要它,例如。 in 指令实际上在 SharedModule 中。然后你必须在导入 ShareModule 时使用 forRoot()
,例如:
import { NgModule, ModuleWithProviders } from '@angular/core';
import { MyDirective } from './my.directive';
import { SomeService } from './some.service';
@NgModule({
declarations: [
MyDirective
],
exports: [
MyDirective
]
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [ SomeService ]
};
}
}
一些其他模块:
import { SharedModule } from './shared/shared.module';
@NgModule({
imports: [
SharedModule.forRoot()
]
})
export class SomeModule {}
希望对您有所帮助:)
我不同意你的说法。
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就是一个class有特定用途的,而在Angular中,我们主要为了三个目的使用服务。1.To 实现独立于任何组件的任何业务逻辑
.
例子
假设您想从 DOB 计算年龄,现在您提供年份
和逻辑可以给你年龄你不需要 HTML 视图来做到这一点
,它是组件 Independent
2。访问共享数据。
在缺乏直接连接的组件(例如兄弟姐妹、孙子等)之间传递数据时,您应该使用共享服务。
您可以使用 RXJS BehaviorSubject
or Subject
进行跨组件通信。
与普通 getters
和 setters
相比,使用 BehaviorSubject
或 Subject
进行跨组件交互的优点是您不需要手动触发获取最新数据的方法。每当数据发生变化时,所有注入服务的组件都会自动得到通知。
3。外部互动
1.Accessing 使用 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 and 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”上。如果我们希望在组件的每个实例及其子实例之间共享依赖项的单独实例,我们可以在组件“提供者”属性 上配置它。*
要获得清晰的图片,请通过 Angular's Hierarchical Dependency Injection system
好吧,建议始终使用根 AppModule 注册应用程序范围的服务,这使得服务成为单例(它会在我们的应用程序存在时存在),但这完全取决于用例。
如果服务的唯一目的是在同级组件之间共享数据并提供几个辅助方法。向组件提供者注册它并使其成为非单例 服务.
好处是在Angular销毁组件的同时,Angular也会销毁服务并释放其占用的内存。@ Credit