3

In Angular 17, There is an Angular library named CommonLib and It contains a WaitSpinnerService which has a private class variable waitSpinner = new EventEmitter();

I use the CommonLib WaitSpinnerService in the AppComponent so I need to import it in the AppModule. I emphasize the WaitSpinnerService is providedIn: 'root'.

In the appComponent's constructor, I listen to the WaitSpinnerService waitSpinner.asObservable via subscribe to turn on or off the wait spinner.

I have a lot of Lazy loaded modules. How it's possible, that when I navigated to a lazy loaded module, always creating a new WaitSpinnerService I see when I navigated to a lazy loaded module, always landed the execution of the constructor of the WaitSpinnerService.

More details about the modules hierarchy: AppModule

SharedModule

  • ModuleAAA (eagerly loaded) import CommonLib, import SharedModule
  • ModuleBBB (eagerly loaded) import CommonLib, import SharedModule
  • ModuleCCC (lazy loaded) import CommonLib, import SharedModule
  • ModuleDDD (lazy loaded) import CommonLib, import SharedModule

Each place the CommonLibModule.forRoot() was used. What will be the right module organization to keep the WaitSpinnerService remains singleton in the App?

2 Answers 2

0

you can use this pattern

@NgModule({
  providers: [WaitSpinnerService]
})
export class CommonLibModule {
  static forRoot(): ModuleWithProviders<CommonLibModule> {
    return {
      ngModule: CommonLibModule,
      providers: [WaitSpinnerService]
    };
  }
}

and then import as like as below

@NgModule({
  imports: [
    CommonLibModule.forRoot()
  ],
  ...
})
export class AppModule {}
Sign up to request clarification or add additional context in comments.

1 Comment

My app contains eager and lazy loaded modules. I import the CommonLibModule in the AppModule, because I use the wait spinner in AppComponent, I listening it is changed or not display or not the wait spinner. But what about The other modules? Currently I importing the CommonLibModule every other modules, does not matter it is lazy or eagerly loaded. But I not provide at all any modules in the application the WaitSpinnerService. As I see more instance will created from the WaitSpinnerServion inspire of the fact that I use ProvidedInű: 'root'.
0

If you service is having provided in root, when you import the library module, this service is automatically added to the root of your application. So no need to add it to the providers array of the app module.


Check if your lazy loaded modules have the spinner service added the the providers array of the component/module. This causes a new service initialisation.


Also try checking if your common module is imported in the lazy loaded modules, with forRoot or for child, this will add the providers at the lazy loaded module level

3 Comments

My "common module" is my library module from my angular library , So I need to import it into Lazy loaded module, if I understand good I no need to add the spinner service in provider array, cause of it can create new service instance. Am I correct?
@L.Kvri yes, that is creating the new instance
@L.Kvri did issue get resolved?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.