什么是angular2中的多提供者 [英] What is multi provider in angular2
问题描述
我知道提供程序是用于从另一个类获取服务,但是什么是多提供程序和令牌呢?
I understand that provider is for getting service from another class but what is multi-provider and token thing?
还有当我们做multi=true
吗?
provide(NG_VALIDATORS, { useExisting: class), multi: true })
推荐答案
multi: true
表示一个提供者令牌提供了一组元素.例如,路由器支持routerLink
,router-outlet
的所有指令均由ROUTER_DIRECTIVES
提供.
如果使用令牌ROUTER_DIRECTIVES
注册了新的提供程序,则它将覆盖以前注册的指令.如果设置了multi: true
(在第一个注册的和新提供程序上),则将新指令添加到先前注册的指令中,而不是覆盖它们.
multi: true
means that one provider token provides an array of elements. For example all directives for router support routerLink
, router-outlet
are provided by ROUTER_DIRECTIVES
.
If a new provider is registered with the token ROUTER_DIRECTIVES
, then it overrides the previously registered directives. If multi: true
(on the first registered and the new provider) is set, the new directives are added to the previously registered directives instead of overriding.
当注入ROUTER_DIRECTIVES
时(constructor(@Inject(ROUTER_DIRECTIVES) directives) {}
),将注入指令实例数组.注入ROUTER_DIRECTIVES
通常没有任何意义.我将其用作示例,因为它是multi: true
.
When ROUTER_DIRECTIVES
is injected (constructor(@Inject(ROUTER_DIRECTIVES) directives) {}
) an array of directive instances is injected. It usually doesn't make sense to inject ROUTER_DIRECTIVES
. I used it just as an example because it is multi: true
.
这篇关于什么是angular2中的多提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!