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

查看:36
本文介绍了angular2 中的多提供者是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

还有当我们执行 multi=true 时?

And also when we do 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) 指令){})时,指令实例的数组被注入.注入 ROUTER_DIRECTICIVES 通常没有意义.我仅将其用作示例,因为它是 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天全站免登陆