Angular 2 How to make singleton service available to lazy loaded modules

前端 未结 2 632
[愿得一人]
[愿得一人] 2020-12-16 22:14

According to my understanding of Angular 2 rc5, to make a service from another module (not AppModule) available as a singleton to every component,

2条回答
  •  醉梦人生
    2020-12-16 22:46

    New to Angular 6 there is a new way to register a provider as a singleton. Inside the @Injectable() decorator for a service, use the providedIn attribute. Set its value to 'root'. Then you won't need to add it to the providers list of the root module, or in this case you could also set it to your MdIconModuleWithProviders module like this:

    @Injectable({
      providedIn: MdIconModuleWithProviders // or 'root' for singleton
    })
    export class MdIconRegistry {
    ...
    

提交回复
热议问题