在 Angular 6 中生成服务时,提供 Injectable 装饰器的目的是什么? [英] What is the purpose of providedIn with the Injectable decorator when generating Services in Angular 6?

查看:24
本文介绍了在 Angular 6 中生成服务时,提供 Injectable 装饰器的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Angular CLI 中生成服务时,它会添加额外的元数据,其中包含provided in"属性,Injectable 装饰器的默认值为root".

When generating services in the Angular CLI, it is adding extra metadata with a 'provided in' property with a default of 'root' for the Injectable decorator.

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

provideIn 到底做了什么?我假设这使服务像整个应用程序的全局"类型单例服务一样可用,但是,在 AppModule 的提供程序数组中声明此类服务不是更清晰吗?

What exactly does providedIn do? I am assuming this is making the service available like a 'global' type singleton service for the whole application, however, wouldn't be cleaner to declare such services in the provider array of the AppModule?

更新:

对于其他任何人,以下段落也提供了另一个很好的解释,特别是如果您只想为功能模块提供服务.

For anyone else, the following paragraph provided another good explanation of it also, in particular if you want to provide your service to only a feature module.

现在有一种新的、推荐的、直接注册提供者的方法在 @Injectable() 装饰器中,使用新的 providedIn属性.它接受 'root' 作为值或您的任何模块应用.当您使用 'root' 时,您的 injectable 将是在应用程序中注册为单例,你不需要将其添加到根模块的提供者中.同样,如果您使用providedIn: UsersModuleinjectable 注册为UsersModule 的提供者,而不将其添加到 Providers模块."-https://blog.ninja-squad.com/2018/05/04/what-is-new-angular-6/

There is now a new, recommended, way to register a provider, directly inside the @Injectable() decorator, using the new providedIn attribute. It accepts 'root' as a value or any module of your application. When you use 'root', your injectable will be registered as a singleton in the application, and you don’t need to add it to the providers of the root module. Similarly, if you use providedIn: UsersModule, the injectable is registered as a provider of the UsersModule without adding it to the providers of the module." - https://blog.ninja-squad.com/2018/05/04/what-is-new-angular-6/

更新 2:

经过进一步调查,我认为只有 providedIn: 'root'

After further investigation, I have decided it is only useful to have providedIn: 'root'

如果你想在除根模块之外的任何模块中provide一个服务,那么你最好在功能模块的装饰器中使用 providers 数组,否则你会被循环依赖所困扰.这里有有趣的讨论 - https://github.com/angular/angular-cli/问题/10170

If you want to provide a service in any module other than the root module, then you are better off using the providers array in the feature module's decorators, otherwise you will be plagued with circular dependencies. Interesting discussions to be had here - https://github.com/angular/angular-cli/issues/10170

推荐答案

如果你使用了providedIn,则注入对象被注册为模块的提供者,而不会将其添加到模块的提供者中.

if you use providedIn, the injectable is registered as a provider of the Module without adding it to the providers of the module.

来自文档

服务本身是一个由 CLI 生成的类,即用@Injectable 装饰.默认情况下,此装饰器已配置使用providedIn 属性,它为服务创建一个提供者.在这种情况下,providedIn: 'root' 指定服务应该是在根注入器中提供.

The service itself is a class that the CLI generated and that's decorated with @Injectable. By default, this decorator is configured with a providedIn property, which creates a provider for the service. In this case, providedIn: 'root' specifies that the service should be provided in the root injector.

这篇关于在 Angular 6 中生成服务时,提供 Injectable 装饰器的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆