什么是angular2中的多提供者

时间:2022-03-24 04:28:54

I understand that provider is for getting service from another class but what is multi-provider and token thing?

我知道提供者是从另一个类获得服务但是什么是多提供者和令牌的东西?

And also when we do multi=true ?

而且当我们做multi = true时?

provide(NG_VALIDATORS, { useExisting: class),    multi: true })

3 个解决方案

#1


18  

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.

multi:true表示一个提供者令牌提供一个元素数组。例如,路由器支持routerLink,router-outlet的所有指令都由ROUTER_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.

当注入ROUTER_DIRECTIVES(构造函数(@Inject(ROUTER_DIRECTIVES)指令){})时,将注入一组指令实例。注入ROUTER_DIRECTIVES通常没有意义。我用它作为一个例子,因为它是多个:真的。

#2


2  

Using multi: true tells Angular that the provider is a multi provider. As mentioned earlier, with multi providers, we can provide multiple values for a single token in DI.

使用multi:true告诉Angular提供者是多提供者。如前所述,对于多提供商,我们可以为DI中的单个令牌提供多个值。

Usages:

用途:

If we have a couple of directives that should automatically be available in our entire application without anyone having to define them in component decorations, we can do that by taking advantage of multi providers and extending what is being injected for PLATFORM_DIRECTIVES.

如果我们有一些指令应该在我们的整个应用程序中自动可用,而无需在组件装饰中定义它们,我们可以通过利用多个提供程序并扩展为PLATFORM_DIRECTIVES注入的内容来实现。

@Directive(...)
class Draggable { }

@Directive(...)
class Morphable { }

@Component(...)
class RootCmp { }

and

// at bootstrap
bootstrap(RooCmp, [
  provide(PLATFORM_DIRECTIVES, {useValue: Draggable, multi: true}),
  provide(PLATFORM_DIRECTIVES, {useValue: Morphable, multi: true})
]);

Details

细节

#3


0  

From the docs:

来自文档:

Creates multiple providers matching the same token (a multi-provider). Multi-providers are used for creating pluggable service, where the system comes with some default providers, and the user can register additional providers. The combination of the default providers and the additional providers will be used to drive the behavior of the system.

创建匹配相同令牌的多个提供程序(多提供程序)。多提供程序用于创建可插入服务,其中系统附带一些默认提供程序,用户可以注册其他提供程序。默认提供程序和其他提供程序的组合将用于驱动系统的行为。

Source

资源

#1


18  

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.

multi:true表示一个提供者令牌提供一个元素数组。例如,路由器支持routerLink,router-outlet的所有指令都由ROUTER_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.

当注入ROUTER_DIRECTIVES(构造函数(@Inject(ROUTER_DIRECTIVES)指令){})时,将注入一组指令实例。注入ROUTER_DIRECTIVES通常没有意义。我用它作为一个例子,因为它是多个:真的。

#2


2  

Using multi: true tells Angular that the provider is a multi provider. As mentioned earlier, with multi providers, we can provide multiple values for a single token in DI.

使用multi:true告诉Angular提供者是多提供者。如前所述,对于多提供商,我们可以为DI中的单个令牌提供多个值。

Usages:

用途:

If we have a couple of directives that should automatically be available in our entire application without anyone having to define them in component decorations, we can do that by taking advantage of multi providers and extending what is being injected for PLATFORM_DIRECTIVES.

如果我们有一些指令应该在我们的整个应用程序中自动可用,而无需在组件装饰中定义它们,我们可以通过利用多个提供程序并扩展为PLATFORM_DIRECTIVES注入的内容来实现。

@Directive(...)
class Draggable { }

@Directive(...)
class Morphable { }

@Component(...)
class RootCmp { }

and

// at bootstrap
bootstrap(RooCmp, [
  provide(PLATFORM_DIRECTIVES, {useValue: Draggable, multi: true}),
  provide(PLATFORM_DIRECTIVES, {useValue: Morphable, multi: true})
]);

Details

细节

#3


0  

From the docs:

来自文档:

Creates multiple providers matching the same token (a multi-provider). Multi-providers are used for creating pluggable service, where the system comes with some default providers, and the user can register additional providers. The combination of the default providers and the additional providers will be used to drive the behavior of the system.

创建匹配相同令牌的多个提供程序(多提供程序)。多提供程序用于创建可插入服务,其中系统附带一些默认提供程序,用户可以注册其他提供程序。默认提供程序和其他提供程序的组合将用于驱动系统的行为。

Source

资源