Angular 注入(inject) token ; 'app.config'是什么?

标签 angular dependency-injection

我正在遵循有关如何提供、然后注入(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/

相关文章:

javascript - Angular2 ng2-auto-complete 实现问题

css - 如何在面板上添加竖线?

c# - 测量 C# 代码的可测试性

java - 将类路径资源注入(inject) Spring 3 bean

javascript - 如何在表单中传递隐藏值?

html - Angular material 2 table - 使用 TemplateRef 和 ngTemplateOutlet 定义列

c# - StructureMap:选择嵌套依赖的具体类型

c# - 使用 MVC 6 在 Web API 中找不到 IServiceCollection

scala - 在 Scala 中使用 monad 读取器的程序架构

angular - 如何修复我的 Angular 测验应用程序中多选题的评分问题?