我正在遵循有关如何提供、然后注入(inject)非类实例的文档;具体来说,我想提供应用程序范围的常量。 The docs为此使用 InjectionToken
,但解释非常简单;考虑第一部分:
app.config.ts
import { InjectionToken } from '@angular/core';
export let APP_CONFIG = new InjectionToken<AppConfig>('app.config');
'app.config' 字符串有什么意义?我在这里看到的每个 InjectorToken 示例都使用了它,但没有一个讨论它的含义或引用。
最佳答案
从参数名称可以看出这是一个描述:
export class InjectionToken<T> extends OpaqueToken {
constructor(desc: string) { super(desc); }
^^^^
toString(): string { return `InjectionToken ${this._desc}`; }
}
它可用于调试目的,如 toString
方法中所示。
它可能是模仿Symbol它还在构造函数中接收描述字符串:
Parameters
- description - Optional Optional, string. A description of the symbol which can be used for debugging but not to access the symbol itself.
请参阅Angular 2 OpaqueToken vs Angular 4 InjectionToken了解为什么引入 InjectionToken
。
关于 Angular 注入(inject) token ; 'app.config'是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44897047/