Ok why the FUCK is my angular app creating a new service when I reroute and throwing away all the state I just carefully put in there?! Does providedIn root mean nothing to you?!

I gave up and just used a global variable

  • 0
    Is it lazy loaded?
  • 1
    You either have lazyload or conflicting provider problems. This is the main reason the common recommendation is "services go in core, core is injected to the app root." You're getting a duplicate token due to the service being explicitly or eagerly provided twice.

    Use the forRoot pattern as seen in Router to sidestep this:


    The other in place option is to write a guarded service that injects a di instance of itself. If injected !== this -> complain loudly.
  • 0
    If your service uses dependencies, then it will be reinstantiated whenever providers for those dependencies change.

    A depends on B depends on C, and they're all provided in root. However, if some module provides C, all three services will be reinstantiated for that module.
Add Comment