angular 6 - creating a (non root) module scoped service

angular 6 - creating a (non root) module scoped service



As far as i know until angular 6 , all the @Ngmodule providers where registered on the root injector and were served in the main bundle even if only lazy loaded modules used them.



The only only exception to this was if we wanted to create a non singleton services in a component level:
https://angular.io/guide/dependency-injection#injectable-ngmodule-or-component



I want to create a singleton service which will be visible only to a specific module (not to the root module), and as a result of that will not be served in the main eagerly loaded bundle.



In saw that in angular 6 the module will no longer need to refer the service via the "providers" , but rather the service will now refer to the module.



This can be done by the "@Injectable" annotation and the "provideIn" attribute.



I didn't find a good and clear example of how can i add a module name which is not 'root', something like this:




@Injectable( provideIn: <MyLocalModule>)
export class SimpleServiceForLocalUseOnly […]



Importing the LazyLoaded module and writing it as "MyLocalModule" in the snippet above is causing a WARNING of Circular dependency.
I can solve this by moving the service to other module, but then i'm losing my initial purpose.



List of searched references:



https://blog.angular.io/version-6-of-angular-now-available-cc56b0efa7a4



https://jaxenter.com/new-angular6-143995.html



https://www.ngdevelop.tech/angular-6-features/



https://blog.ninja-squad.com/2018/05/04/what-is-new-angular-6/



http://ankitsharmablogs.com/getting-started-with-angular-6-0/



https://www.youtube.com/watch?v=Xr5l7lT--YU




2 Answers
2



There seems to be some issues regarding circular dependency if we follow this setup according to the official docs:


import Injectable from '@angular/core';
import HeroModule from './hero.module';
import HEROES from './mock-heroes';

@Injectable(
// we declare that this service should be created
// by any injector that includes HeroModule.

providedIn: HeroModule,
)
export class HeroService
getHeroes() return HEROES;



You can either ignore the warnings that the compiler raises due to circular dependency between the Module, Service and Component. Or, fallback to the previous methods used in Angular 5.



Register service as providers in lazy loaded module, note that you should not import lazy loaded modules in your root app module:


@NgModule(
imports: [
RouterModule.forChild([ path: '', component: LazyComponent ]),
],
declarations: [
LazyComponent
],
providers: [YourServiceHere]
)
export class LazyLoadedModule





When i do it i get: WARNING in Circular dependency detected: HeroService -> HeroModule -> HeroComponent -> HeroService
– Rotemya
May 9 at 10:06





it could be a problem with the Angular library itself, basically cause this setup requires circular imports of service, module and component. If it is just a WARNING and your app is working then I think it is safe to ignore. Or you could just not use the way to set up module-scoped services. Just set up the service in the providers array in your lazy loaded module.
– Joshua Chan
May 9 at 10:28





But my initial purpose was not to include this service in the main bundle, adding it to the modules providers array will end up with this result.
– Rotemya
May 9 at 10:47





are you lazy loading your module? From my understanding, services registered in lazy loaded modules are lazily loaded as well.
– Joshua Chan
May 9 at 10:51





Yes, this module is lazy loaded, and services which are registered on the module's "providers" are added to the root injector and not being lazy loaded (check you're main bundle and see). As far as i understand in angular 6 it was changed (in the 2nd link there is a good cover of this).
– Rotemya
May 9 at 10:57



To provide a service in a specific module using the newer syntax you just do something like this:


import Injectable from "@angular/core";
import YourModule from "path/to/your.module";

@Injectable(
providedIn: YourModule
)
export class SomeModuleScopedService



reference: https://angular.io/guide/providers#providedin-and-ngmodules






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế