Angular 6 Services: providedIn: 'root' vs CoreModule

后端 未结 7 902
猫巷女王i
猫巷女王i 2021-02-01 02:40

With Angular 6, below is the preferred way to create singleton services:

import { Injectable } from \'@angular/core\';

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


        
相关标签:
7条回答
  • 2021-02-01 03:18

    And what about interceptors and guards? This things can be global I guess. I agree with your consideration about CoreModule doesn't have declarations and only pure services, but I think Guards and Interceptors should be considerate too.

    0 讨论(0)
  • 2021-02-01 03:21

    When Angular engine starts initiating a component, and you have declared services in your constructor, angular tries to pass the instances of the declared services to the component. So angular should somehow know where can be taken instances, there is a point where providers array comes.

    When you declare needed services without providedin: core pair, we create non singleton instance of the service. which means that, when angular destroys component, services declared in the component will be destroyed too.

    As mentioned before, there are two ways to declare application level singleton services: 1. declare them into providers of related module; 2. declare them into providers of a component with providedIn: core pair.

    also interesting note about @Injectable decorator. this is only required when service injects another service. for example if you inject http service in your service, you have to add @Injecrable decorator.

    this is how dependency injection works.

    0 讨论(0)
  • 2021-02-01 03:25

    I would still keep CoreModule for single use components and Http interceptors, with the providedIn property is now the recommended way to register singleton services, just for clarity I would put all my singleton services under core/services directory

    0 讨论(0)
  • 2021-02-01 03:30

    That would be true if the CoreModule only contained services. However, it does include other things such as single use components.

    From Angular Docs:

    Do gather application-wide, single use components in the CoreModule. Import it once (in the AppModule) when the app starts and never import it anywhere else. (e.g. NavComponent and SpinnerComponent).

    0 讨论(0)
  • 2021-02-01 03:34

    I think creating a core module became obsolete since a service can define itself to be registered to root via providedIn: root

    The (old!) angular style guide recommended to create a core module till v6: https://v6.angular.io/guide/styleguide

    But since v7 there is no recommendation anymore to create a core module. I think it is exactly for that reason (providedIn: root does the job).

    0 讨论(0)
  • 2021-02-01 03:34

    provideIn: root this could be useful in those scenarios where you have multi-application projects. In such kind of scenarios, you have an angular workspace with multiple projects inside it.

    And, you would never want your project to have some kind of dependencies on the workspace's core module as it won't allow you to run your project independently.

    In such a case, you keep your services in the library and use providedIn: 'root' for making them singleton.

    0 讨论(0)
提交回复
热议问题