I'm on Angular 8, simple app, with a Lazy loaded module and a service providedIn: 'root': everything's working.
Now I'd like to provide the service in the LazyLoadedModule, so that the service is provided only when the lazy loaded module is downloaded on the respective route. So I've updated my service as the following:
@Injectable({
providedIn: LazyModule
})
export class MyService {... }
This service is injected in a component declared in the LazyModule. Now I got the following error:
WARNING in Circular dependency detected: src\app\lazy-module\lazy.service.ts -> src\app\lazy-module\lazy.module.ts -> src\app\lazy-module\lazy-routing.module.ts -> src\app\lazy-module\lazy-component\lazy.component.ts-> src\app\lazy-module\lazy.service.ts
So basically:
- the lazy.service has a dependency on the lazy.module (caused by the providedIn I guess)
- the lazy-module has a dependency on lazy-routing module (cause the routing file is imported in its module)
- the lazy-routing module has a dependency on some components since i'm eagerly loaded some component if route is '' for example
- the component has a dependency of lazy.service since it's injected in the constructor...
I'm trying to replicate on Stackblitz but I got another error: No provider for LazyService!
Lazy.Service.ts
@Injectable({
providedIn: LazyModule
})
export class LazyService { ... }
Lazy-routing.module
const ROUTES: Routes = [
{
path: '',
component: LazyComponent
}
];
@NgModule({
imports: [RouterModule.forChild(ROUTES)],
exports: [RouterModule]
})
export class LazyRoutingModule { }
Lazy-component.ts
export class LazyComponent implements OnInit {
constructor(private lazyService: LazyService) { }
}
Any ideas how I can solve this?
Thanks!