将提供程序注入另一个提供程序 [英] Inject provider into another provider

查看:111
本文介绍了将提供程序注入另一个提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个名为Comp的组件和两个名为P1P2@Injectable提供程序.

Let's say we have a Component called Comp and two @Injectable providers called P1 and P2.

P1需要P2的实例.将P1注入到Comp中.

P1 needs an instance of P2. P1 is injected into Comp.

如果我像这样在Comp上声明两个提供程序,则效果很好:

It's working perfectly if I declare both providers on Comp like this:

@Component ({
    providers: [P1, P2]
})
export class Comp { ... }

现在,我想做的是声明P1直接在P1内部需要P2:

Now, what I would like to do is to declare that P1 needs P2 directly inside P1:

@Component ({
    providers: [P1]
})
export class Comp { ... }


@Injectable(/** Inject P2 here **/)
export class P1 { ... }

如何实现?

推荐答案

实际上,只能在组件级别或引导应用程序时配置注入器.

In fact, injectors can be only configured at the component level or when bootstrapping the application.

在组件级别设置提供程序时,处理中涉及的每个类都可以访问以下提供程序:子组件,服务.但是您不能自己为服务配置提供程序.

When you set providers at the component level, every classes involved in the processing will have a access to these providers: sub components, services. But you can't configure providers for services themselves.

如果您在引导程序级别上配置提供程序(调用bootstrap函数时),则应用程序中的所有元素都将能够使用这些提供程序.

If you configure providers at the bootstrap level (when calling the bootstrap function), all elements in the application will be able to use these providers.

事实上,Angular2的依赖注入器利用了分层注入器.这意味着,如果未在某个级别上找到提供程序,则将在上面的级别中进行搜索,依此类推.

In fact, dependency injector of Angular2 leverages hierarchical injectors. This means that if the provider isn't found at a level, it will be look for in the level above and so on.

以下是所有这些元素及其相关性的概述:

Here is an overview of all these elements and there relations:

Application
(providers defined in bootstrap)
     |
AppComponent
(providers defined in the providers attribute)
     |
ChildComponent
(providers defined in the providers attribute)
  getData()     --- Service1 --- Service2

要在Service1中使用Service2,必须在提供者树中找到相应的提供者.

To be able to use Service2 in Service1, the corresponding provider must be found in the providers tree.

在这种应用中,我们有三个喷射器:

In such application, we have three injectors:

  • 可以使用bootstrap函数的第二个参数
  • 配置的应用程序注入器
  • 可以使用此组件的providers属性配置的AppComponent进样器.它可以看到"在应用程序注入器中定义的元素.这意味着,如果在此提供程序中找不到提供程序,则会自动在此父注入器中查找.如果在后者中未找到,将引发未找到提供者"错误.
  • ChildComponent进样器将遵循与AppComponent相同的规则.要注入在组件中执行的注入链中涉及的元素,将首先在此注入器中查找提供者,然后在AppComponent中寻找提供者,最后在应用程序中寻找
  • .
  • The application injector that can be configured using the second parameter of the bootstrap function
  • The AppComponent injector that can be configured using the providers attribute of this component. It can "see" elements defined in the application injector. This means if a provider isn't found in this provider, it will be automatically look for into this parent injector. If not found in the latter, a "provider not found" error will be thrown.
  • The ChildComponent injector that will follow the same rules than the AppComponent one. To inject elements involved in the injection chain executed forr the component, providers will be looked for first in this injector, then in the AppComponent one and finally in the application one.

此答案可以为您提供有关分层注射器的更多详细信息:

This answer could give you more details about the hierarchical injectors:

这篇关于将提供程序注入另一个提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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