What is the purpose of providedIn with the Injectable decorator when generating Services in Angular 6?

后端 未结 6 627
予麋鹿
予麋鹿 2020-12-12 10:06

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.

<         


        
6条回答
  •  长情又很酷
    2020-12-12 10:49

    providedIn tells Angular that the root injector is responsible for creating an instance of the your Service. Services that are provided this way are automatically made available to the entire application and don't need to be listed in any module.

    Service classes can act as their own providers which is why defining them in the @Injectable decorator is all the registration you need.

提交回复
热议问题