Skip to content

Instantly share code, notes, and snippets.

@UserGalileo
Created August 24, 2021 23:03
Show Gist options
  • Save UserGalileo/a645bcea35162d05b97d054f14a94af3 to your computer and use it in GitHub Desktop.
Save UserGalileo/a645bcea35162d05b97d054f14a94af3 to your computer and use it in GitHub Desktop.
Angular InjectionToken
// Injection Token
const APP_CONFIG = new InjectionToken<AppConfig>('Your description!');
const appConfig: AppConfig = { ... }
// The provider's configuration.
// At this point, whoever injects APP_CONFIG gets this object.
// But APP_CONFIG is not a class! How do we grab it?...
@NgModule({
...,
providers: [{ provide: APP_CONFIG, useValue: appConfig }]
})
export class ConfigModule {}
// ...We grab it like this!
@Component({ ... })
export class MyComponent {
constructor(@Inject(APP_CONFIG) config: AppConfig) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment