什么是angular2中的多提供者 [英] What is multi provider in angular2

查看:110
本文介绍了什么是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表示一个提供者令牌提供了一组元素.例如,路由器支持routerLinkrouter-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆