Angular 2 个容器

Angular 2 di container

我正在研究 Angular 2,我想知道团队如何处理组件中的依赖注入?

说明

看来Ng2是通过typescript基于ES7装饰器的。事实上,class 装饰器是 launch (我的意思是,装饰器的代码)仅在实例化 class 时被解释。

那么,团队如何处理 component/service 注入?

例子

从文档中查看这段代码:

class AComponent {
  constructor(@Inject(MyService) aService:MyService) {}
}

在调用class的构造函数时进行注入。这意味着 MyService 具有特定的 decorator/specific 元数据(如 @Injectable),允许他被注入 no ?

在这种情况下,这意味着有一个服务注册表,可以通过 @Inject 注释访问它们的列表。

但这意味着框架能够在运行时(这是重要的词)创建和加载这个存储库,这与装饰器调用不匹配(在class 注释的实例化)?

这是否意味着每个 component/services 注射剂至少实例化一次?或者是否存在其他东西可以在运行时注入或执行一些装饰器来填充服务registry/container?

感谢提前

In fact, class decorator is "launch" (I mean, the code of the decorator) is only interpreted when the class is instanciated.

喷油器是的。但是 Component 不,这就是你所缺少的拼图。

declare var Component: any;
declare var Inject: any;
declare type MyService = any;
declare var MyService: any;

@Component
class AComponent {
  constructor(@Inject(MyService) aService:MyService) {}
}

@Component 调用将在 definition 时进行。这将允许 angular 注册组件。当有人请求所述组件时,Constructor @Inject 将执行,因此 angular DI 将启动。