I have a plan to develop a standalone Angular service for Angular 8+, and I've been researching how to ensure it is tree-shakable.
My understanding is that we just need to do the following:
@Injectable({
providedIn: 'root',
useFactory: () => new Service('dependency'),
})
export class Service {
constructor(private dep: string) {
}
}
By providing this information, Angular can properly create the service.
Is this all that needs to be done? I noticed other NPM modules like this one using ModuleWithProviders
as shown below:
export class Module {
static forRoot(): ModuleWithProviders {
return {
ngModule: Module,
providers: [
StripeScriptTag
],
}
}
However, I believe this additional step is unnecessary because the providers
array still does not specify how to initialize the service.