可以在Angular中创建一个不是单例的服务吗?

huangapple go评论57阅读模式
英文:

Is it possible make a service in angular without singleton?

问题

早上好,

我知道在Angular中,大多数情况下服务都是作为单例使用的,以便能够与其他模块共享同一个服务实例。

然而,我有一个需求,我希望每个我在Providers部分提供服务的模块都是一个新的实例。

我解释一下:我有一个名为report-params-store-service的服务,它是用来保存报告参数数据的。所以我想要在例如INSS报告、IRRF报告、FGTS报告中注入report-params-store-service,但在它们每一个中都是一个隔离的服务,不是单例的,因为我不想在由同一个report-params-store-service注入的不同报告之间共享参数数据。

我只想避免重复使用report-params-store-service的代码行,不必为每个报告都创建一个新的服务实例。

这可能吗?怎么做?

我将这些服务放在每个我需要的模块的providers中,但它表现得像一个单例服务,将数据共享给不同的模块。

英文:

Morning,

I know that most use of Services in Angular is as singleton, to be able to share the same instance of the service with others modules.

However, I have a need in which I want each module that I provide the service in the section Providers be a new instance.

I explain: I have the services report-params-store-service that was created to save data params of reports. So I would like of injecting the report-params-store-service in, for example, INSS Reports, IRRF Reports, FGTS Reports, but that in each one of them was a isolated service, without singleton, because I am not wanna share datas params among the different reports that was injected by the same service report-params-store-service.

I am only wanna avoid repeating line code of the report-params-store-service having that to create one of him for each reports that I have.

Is it possible ? And how ?

I put the services in the providers in the each module that I needed but It behaved as a singleton service, sharing the data among the different modules.

答案1

得分: 2

在你的服务文件中进行以下更改:

从:

@Injectable({
   providedIn: 'root',
})

改为:

@Injectable()

然后在每个模块中提供你的服务,像这样:

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [ReportParamsStoreService ],
})
export class FeatureModule {}
英文:

In your service file change the following:

From:

@Injectable({
   providedIn: 'root',
})

To:

@Injectable()

Then provide your service in every module like this:

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [ReportParamsStoreService ],
})
export class FeatureModule {}

答案2

得分: 1

你的用例在Angular的文档中有详细描述:https://angular.io/guide/providers#limiting-provider-scope-by-lazy-loading-modules

当使用 providedIn: 'any' 时,所有急加载模块共享一个单例实例;但是,惰性加载的模块会每个都有自己独特的实例,如下图所示。

这意味着你应该将 providedIn: 'any' 设置为你的服务,并将其添加到需要的 providers 数组中(可以是在你的模块或独立组件中)。

请注意,这仅适用于惰性加载的模块,这通常是最佳实践,但从你的问题中并不清楚是否在使用。如果你没有使用它们,请参考Khumo Mogorosi的答案,因为这是一种更通用的方法。

英文:

Your use-case is described in detail in angular's documentation: https://angular.io/guide/providers#limiting-provider-scope-by-lazy-loading-modules

> With providedIn: 'any', all eagerly loaded modules share a singleton instance; however, lazy loaded modules each get their own unique instance, as shown in the following diagram.

This mean you should set providedIn: 'any' to your service and add it to the providers array where needed (in your module or in your standalone component).

Please be aware that this applies only to lazy loaded modules, which is in general a best practice, but from your question it's not clear if you are using them. If you don't have them in place, please refer to Khumo Mogorosi's answer, since this is a more generic approach.

huangapple
  • 本文由 发表于 2023年7月13日 17:57:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定