Currently, I am in the process of developing an angular library that includes an internal service. The service is defined as follows:
I have utilized providedIn
to ensure it is tree-shakable and did not opt for providedIn:'root'
as it is solely used within the module's scope.
@Injectable({
providedIn: MyChartModule,
})
export class LineChartLibService {
However, when attempting to incorporate forRoot
in the module definition to maintain a single instance during lazy loading, a Circular dependency issue arose.
export class MyChartModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: MyChartModule,
providers: [LineChartLibService],
};
}
}
In such scenarios, what would be the best approach? Is it feasible to have a lazy load-able, tree-shakable service within a library?
WARNING: Circular dependency: dist\my-chart\esm2015\lib\my-chart.module.js -> dist\my-chart\esm2015\lib\line-chart\line-chart.component.js
-> dist\my-chart\esm2015\lib\line-chart-lib.service.js -> dist\my-chart\esm2015\lib\my-chart.module.js