Search code examples
angulardependency-injectioninjectangular-dependency-injectionangular-injector

Angular 18 Multiple instance of same service


I've:

service DataLoader who read/write data in DB.

service Report who instances DataLoader using inject

standalone component who inject DataLoader and Report too.

At this point, only one instance of DataLoader its running, and I need to have inside Report, exclusive instance of DataLoader, my conflict its because when component tries to use some functions, the values inside still from the last usage(Report), and I need to stay values from component instance.

In resuming, I think I need multiple instances of DataLoader and somehow inject inside in others services too


Solution

  • Find solution: I created one module and put inside all services who share one single instance. So in DataLoader, I let @Injectable({ providedIn: 'any' }), and in component, add to providers DataLoader (for individual) and in imporrts, I put this new module.