带有字符串标记的 Angular 动态 DI

标签 angular dependency-injection angular-di

似乎 Angular 6(4+?)要求 token 对象 是唯一的,以便 DI 工作。然而,我想使用一个动态 token ,由模板 html 代码提供,这将允许我的新指令按名称解析服务。

目前代码:

this.service = this.injector.get(new InjectionToken<IServiceRef>(tokenName));

失败:

Error: StaticInjectorError(AppModule)[InjectionToken the_token_name]: 

当我用旧的 depricated(在 Angular 4 中)Inj​​ector.get 函数替换我的代码时,它工作正常,因为注入(inject)器比较名称(我确实在 View 中提供该名称的服务......)。然而,使用新的 DI 我无法实现我想要的。

那么,如何应对呢?

最佳答案

您必须为您的 token 使用全局存储对象。我建议您使用 map 。

export const tokens: Map<string, InjectionToken<IServiceRef>> = new Map();
tokens.set('tokenName', new InjectionToken<IServiceRef>('tokenName'));

您必须使用 map 对象来声明提供者。

@NgModule({
     providers: [
          {provide: tokens.get('tokenName'), useValue: new Service()}
     ]
 );

您现在可以通过字符串值查找 token 。

this.service = this.injector.get(tokens.get(the_token_name));

我不知道这在 Angular 6 中发生了变化,但我确实记得文档中说 token 是按值引用的。这意味着 DI 使用 === 来匹配依赖项。

如果您通过 token 名称进行匹配,您将在 DI 中遇到冲突。例如,许多库都声明了一个“文档”标记。所以你不想使用字符串名称。因为碰撞很难找到和解决。

关于带有字符串标记的 Angular 动态 DI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52992187/

相关文章:

angular - 设置环境变量ionic 3

angular - Cypress 测试 - Google Places 自动完成功能不工作

javascript - 从 Angular2 组件传递 Json 到 Node.js

asp.net-mvc - Fluent 验证和 IoC(独特字段)

angular - NG0200 : Circular dependency in DI detected for ApplicationRef

Angular 8 : "Please add a @NgModule annotation" when including a library into another library

dependency-injection - 在 ServiceStack API 应用程序中使用的正确 'per request' Simple Injector 生活方式是什么?

javascript - 在创建函数存储在散列中的 JS 对象时,“this”被限定为窗口对象

angular - 如何为多个组件保留一个服务实例?

angular - Angular 中的 StaticInjector 与 ReflectiveInjector