简体   繁体   English

如果在 2 个延迟加载模块中使用了 Angular 服务,则为providedIn 设置什么

[英]What to set for providedIn, if Angular service is used in 2 lazy loaded modules

Since Angular 6 recommended way is to use providedIn property of Injectable .由于 Angular 6 推荐的方法是使用Injectable providedIn属性。

But what if need to use service in 2 lazy loaded modules?但是如果需要在 2 个延迟加载模块中使用服务怎么办?
Does creating shared module and then import shared module in both lazy loaded modules consider still the best practice?创建共享模块然后在两个延迟加载的模块中导入共享模块是否仍然认为是最佳实践?

Something like this(following this link to avoid circular reference warning):像这样的东西(按照链接以避免循环引用警告):

//Shared module
@NgModule({})
export class SharedModule { }

//Shared service
@Injectable({
  providedIn: SharedModule
})
export class SharedService { }


//Lazy loaded modules A and B
@NgModule({
  imports: [SharedModule]
})
export class LazyModuleA { }

@NgModule({
  imports: [SharedModule]
})
export class LazyModuleB { }

Edit based on first answer: There will be quite (40-80) services.根据第一个答案进行编辑:会有相当(40-80)的服务。

I don't thing it is a good idea.Because you inject a service in an injector for situations that may or may not happen .When you inject this service in ShareModule, this service is alive until your app is alive.Let's imagine a situation that you've got lots of these scenarios, you inject a lot of services in injector that is memory consuming.我不认为这是一个好主意。因为你在注入器中注入一个服务来应对可能发生或可能不会发生的情况。当你在 ShareModule 中注入这个服务时,这个服务一直存在,直到你的应用程序还活着。让我们想象一个情况你有很多这样的场景,你在注入器中注入了大量消耗内存的服务。 I think it's better to inject this service at level of lazyLoad modules.我认为最好在 lazyLoad 模块级别注入此服务。

This is what I have observed with services in Angular 6(and above)这是我在 Angular 6(及更高版本)中观察到的服务

You can safely use "providedIn: 'root'"您可以安全地使用“providedIn:'root'”

The only catch is please do not add it in imports in the app.module (or whatever entry module you have).唯一的问题是请不要将它添加到 app.module (或您拥有的任何入口模块)的导入中。 If you add it, it will load immediately.如果添加它,它将立即加载。

Else, if you use "providedIn: 'root'" and it is getting used in any number of modules, it will not load until any of the module that uses that service loads.否则,如果您使用 "providedIn: 'root'" 并且它在任意数量的模块中使用,则在使用该服务的任何模块加载之前它不会加载。

Once loaded, it shares the same reference across modules whoever is using it.加载后,它在使用它的模块之间共享相同的引用。

Example description:示例说明:

Service A(provided in root)服务 A(以 root 身份提供)

app.module应用模块

lazy-module-1(does not use service A)懒惰模块1(不使用服务A)

lazy-module-2(uses service A)懒惰模块2(使用服务A)

lazy-module-3(does not use A)懒惰模块3(不使用A)

lazy-module-4(uses A)懒惰模块4(使用A)

service A will load when either lazy-module-2 or lazy-module-4 is loaded.服务 A 将在加载 lazy-module-2 或 lazy-module-4 时加载。 Once loaded, it stays in the app加载后,它会留在应用程序中

Do let me know if you want a demo app on the same.如果您想要一个演示应用程序,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 依赖于延迟加载模块中的“providedIn” - Angular dependency with “providedIn” in lazy loading modules 角度服务装饰器提供了对延迟加载的根效果 - Angular service decorator providedIn root effect on lazy loading 提供Angular 7服务:'root' - Angular 7 Service providedIn: 'root' Angular 2如何为延迟加载的模块提供单例服务 - Angular 2 How to make singleton service available to lazy loaded modules 在 Angular 中注入 URL 6 延迟加载模块之间共享的服务 - Inject URL in Angular 6 Service shared across lazy loaded modules 在 Angular 5 中卸载延迟加载的模块 - Unloading lazy loaded modules in Angular 5 Angular 6/7延迟加载模块,具有其他延迟加载模块所需的服务 - Angular 6/7 lazy loaded modules with services required for another lazy loaded modules 在延迟加载的模块中加载小型 Angular 模块 - Loading small Angular Modules in Lazy loaded modules providedIn 属性在 Angular 服务不工作 - providedIn Property In Angular Service Is not working Angular:如果一个懒加载模块调用另一个懒加载模块的服务会发生什么? - Angular: What will happen if a lazy loaded module calls service from another lazy loaded module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM