Skip to content

Instantly share code, notes, and snippets.

@alxhub
Created March 16, 2018 19:04
Show Gist options
  • Save alxhub/2aa19e8f5fea04072088348ce20e2454 to your computer and use it in GitHub Desktop.
Save alxhub/2aa19e8f5fea04072088348ce20e2454 to your computer and use it in GitHub Desktop.
Tree-shakeable providers Example
// Here's a fictitious LayoutService without a module!
//
// It'll automatically be available in the application's
// root injector (as if it was provided in the AppModule).
//
// Even though it's in the root injector, if this service
// is only used in a lazy loaded context, it will be lazy
// loaded!
@Injectable({
providedIn: 'root',
useClass: DefaultLayoutService
})
export abstract class LayoutService {
getLayout(element: any): Layout;
}
@Injectable()
export class DefaultLayoutService {
...
}
// If you still want this service to behave as if it was
// provided directly in a module, you can do that too:
@NgModule({
providers: [
// No provider needed - @Injectable will reference the @NgModule
// and behave as if this provider was declared:
//
// {provide: LayoutService, useClass: DefaultLayoutService}
]
})
export class LayoutModule {}
@Injectable({
providedIn: LayoutModule,
useClass: DefaultLayoutService
})
export abstract class LayoutService {...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment