Reputation: 40916
I am following the docs on how to provide, then inject a non-class instance; specifically I want to provide application wide constants. The docs use the InjectionToken
for this, but the explanations are very bare; consider the first part:
app.config.ts
import { InjectionToken } from '@angular/core';
export let APP_CONFIG = new InjectionToken<AppConfig>('app.config');
What's the point of the 'app.config' string? Every example of InjectorToken that I see here uses that, but none discusses what it means or refers to.
Upvotes: 4
Views: 2130
Reputation: 105517
It's a description as can be seen from the parameter name:
export class InjectionToken<T> extends OpaqueToken {
constructor(desc: string) { super(desc); }
^^^^
toString(): string { return `InjectionToken ${this._desc}`; }
}
It can be used for debugging purposes as it's seen in the toString
method.
It is probably modeled after Symbol which also receives description string in the constructor:
Parameters
- description - Optional Optional, string. A description of the symbol which can be used for debugging but not to access the symbol itself.
Refer to the Angular 2 OpaqueToken vs Angular 4 InjectionToken to understand why the InjectionToken
was introduced.
Upvotes: 2