Angular2 - 注入@Injectable [英] Angular2 - inject into @Injectable

查看:104
本文介绍了Angular2 - 注入@Injectable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Angular2 应用,其中包含服务,用于从API获取数据。在这个示例之后,我想要创建一个单独的文件,其中应包含一些配置数据。我的问题是我的服务有一个 @Injectable()装饰器,我不确定我是否可以传递提供元数据中的数组,我将在其中注入配置,如教程中所示。任何人都遇到过这样的问题,欢迎分享他的解决方案:)

I have an Angular2 app with a service that is used for getting a data from an API. Following this example, I want to create a separate file which should contain some configuration data. My problem is that my service has an @Injectable() decorator and I'm not sure if I can pass it a provide array in the metadata in which I will inject the configuration as shown in the tutorial. Anybody ever faced such a problem is welcome to share his solution :)

推荐答案

事实上,Angular2利用分层喷射器和喷射器连接组件。简而言之,您只能在组件( providers 属性)或应用程序级别( bootstrap 函数)上定义提供程序。

In fact, Angular2 leverages hierarchical injectors and injectors are linked to components. In short you can define providers only on components (providers property) or at application level (bootstrap function).

关于服务,他们将能够使用对发起呼叫的组件可见的提供商,但您无法在其级别定义提供商。

Regarding services, they will be able to use providers that are visible for the component that initiated the call but you can't define providers at their levels.

以下是一个示例:

Application
     |
AppComponent
     |
ChildComponent
  getData()     --- Service1 --- Service2

In这样的应用程序,我们有三个注入器:

In such application, we have three injectors:


  • 可以使用 bootstrap的第二个参数配置的应用程序注入器 function

  • 可以使用供应商配置的 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.

这意味着当尝试将 Service1 注入 ChildComponent 构造函数,Angular2将查看 ChildComponent 注入器,然后进入 AppComponent 一个最后进入应用程序之一。

This means that when trying to inject the Service1 into the ChildComponent constructor, Angular2 will look into the ChildComponent injector, then into the AppComponent one and finally into the application one.

由于 Service2 需要注入 Service1 ,将完成相同的分辨率处理: ChildComponent injector, AppComponent one和application one。

Since Service2 needs to be injected into Service1, the same resolution processing will be done: ChildComponent injector, AppComponent one and application one.

这意味着 Service1 Service2 都可以使用组件的 providers 属性和 bootstrap 函数的第二个参数,根据您的需要在每个级别指定应用程序注入器。

This means that both Service1 and Service2 can be specified at each level according to your needs using the providers attribute for components and the second parameter of the bootstrap function for the application injector.

这个答案可以帮到你:

  • What's the best way to inject one service into another in angular 2 (Beta)?

这篇关于Angular2 - 注入@Injectable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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