Created
August 24, 2021 23:03
-
-
Save UserGalileo/a645bcea35162d05b97d054f14a94af3 to your computer and use it in GitHub Desktop.
Angular InjectionToken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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