Opaque token - AngularBuildUp/angular-microworkshops-authorization GitHub Wiki
You can also use a token to register an object into Angular's DI and also use that to resolve.
This works extremely well for non-class dependencies. Angular DI doesn’t work for non-class dependencies, like the const HERO_DI_CONFIG
.
export interface AppConfig {
apiEndpoint: string;
title: string;
}
export const HERO_DI_CONFIG: AppConfig = {
apiEndpoint: 'api.heroes.com',
title: 'Dependency Injection'
};
import { OpaqueToken } from "@angular/core";
export let APP_CONFIG = new OpaqueToken("app.config");
You register the token with the name passed in the constructor. This way Angular will know to resolve this.
You register using the OpaqueToken
providers: [ { provide: APP_CONFIG, useValue: HERO_DI_CONFIG } ]
Usage:
constructor(@Inject(APP_CONFIG) private _config: AppConfig) { … }