简体   繁体   English

Angular 5 全局组件及其服务

[英]Angular 5 global components and their services

Looking through Angular's docs, specifically the style guide, they lay out both a shared module and a core module.查看 Angular 的文档,特别是样式指南,他们列出了共享模块和核心模块。 According to the docs, services should be placed in the core module and components in the shared module.根据文档,服务应放置在核心模块中,而组件应放置在共享模块中。 My question is if I have a component, say a custom file upload component, and it also has a corresponding service, where should that component and service reside?我的问题是,如果我有一个组件,比如说自定义文件上传组件,并且它也有相应的服务,那么该组件和服务应该驻留在何处? It seems to make sense to keep the two together since the service is used with the component, however, this doesn't seem to follow Angular standards.由于服务与组件一起使用,因此将两者保持在一起似乎很有意义,但是,这似乎并不遵循 Angular 标准。 Anyone come across this issue?有人遇到过这个问题吗?

According to Angular Style Guide, Angular doesn't really recommend providing a service in the SharedModule .根据 Angular Style Guide,Angular 并不真正推荐在SharedModule提供服务。 The reason for that is, whichever module imports the SharedModule , will get a separate instance of that shared service.原因是,无论哪个模块导入SharedModule ,都将获得该共享服务的单独实例。 This isn't really acceptable in case of a Stateful Service.在有状态服务的情况下,这实际上是不可接受的。 But in your case, I'm not really sure if the Service will contain any state data.但是在您的情况下,我不确定该服务是否会包含任何状态数据。 So I think, it should be safe for you to add this Component and this Service to the SharedModule itself.所以我认为,将这个 Component 和这个 Service 添加到 SharedModule 本身应该是安全的。

Again, considering that this service is not going to have any state data.同样,考虑到此服务不会有任何状态数据。

Also, Angular recommends providing services inside the CoreModule because it also recommends making the CoreModule only importable by just a single module(generally the AppModule ).此外,Angular 建议在CoreModule内提供服务,因为它还建议使CoreModule只能由单个模块(通常是AppModule )导入。 That's why it also recommends creating a Guard to prevent re-importing of the CoreModule.这就是为什么它还建议创建一个 Guard以防止重新导入 CoreModule。

Look up to Providers and singleton-services .查找Providerssingleton-services In usual way that:以通常的方式:

  • if you want singleton service you must provide it in root module, eg: router如果你想要单例服务,你必须在根模块中提供它,例如:路由器
  • if you need service instance per component instance, then you provide that service in component definition, and you can pass that service instance to all child components:如果您需要每个组件实例的服务实例,那么您可以在组件定义中提供该服务,并且您可以将该服务实例传递给所有子组件:

    @Component({ /* . . . */ providers: [UserService] }) @Component({ /* . . . */ providers: [UserService] })

    In case of yours example: if every upload component needs new instance of service, service is defined as @Injectable class, imported to upload component source and registered as a provider in component decorator providers section.在您的示例中:如果每个上传组件都需要新的服务实例,则服务被定义为 @Injectable 类,导入到上传组件源并在组件装饰器提供者部分注册为提供者。

This is a good question and I've got it constantly asked by colleagues in my company.这是一个很好的问题,我公司的同事经常问我这个问题。 As you said the best practice in official docs is to have CoreModule and SharedModule that each in charge of different concerns.正如您所说,官方文档中的最佳实践是让CoreModuleSharedModule分别负责不同的问题。 Most of the time it makes sense to put your services into "Core" and components/directives/pipes etc. into "Shared".大多数情况下,将您的服务放入“核心”并将组件/指令/管道等放入“共享”是有意义的。

But sometimes we do have exceptions and want to do the following:但有时我们确实有例外并希望执行以下操作:

  1. A service in SharedModule to manage the states/configurations/behaviors for the counterpart components/directives/pipes SharedModule一项服务,用于管理对应组件/指令/管道状态/配置/行为
  2. A component in CoreModule that only exists in the root level or dynamically created during runtime. CoreModule中的一个组件,它只存在于根级别或在运行时动态创建。

For scenario 1 you can have the legacy forRoot() method for your module to make sure your service only gets initialized once.对于场景 1,您可以为您的模块使用传统的forRoot()方法,以确保您的服务仅初始化一次。 Also it's worth mentioning that with Angular 6 you can use the new provideIn: 'root' syntax which simplifies this purpose.另外值得一提的是,在 Angular 6 中,您可以使用新的provideIn: 'root'语法来简化此目的。 You can find many resources online.你可以在网上找到很多资源。

For scenario 2 you usually put it into entryComponents .对于场景 2,您通常将其放入entryComponents

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM