本文介绍了没有供应商ObservableDataService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误否提供商ObservableDataService

I got error with "No provider for ObservableDataService"

ObservableDataService
https://github.com/sanex3339/bannerscreator/blob/master/resources/assets/typescript/Services/ObservableDataService/ObservableDataService.ts

服务,其中 ObservableDataService 将注入:
https://github.com/sanex3339/bannerscreator/blob/master/resources/assets/typescript/Services/UploadedTemplatesService/UploadedTemplatesService.ts

我描述里面'供应商' @Component 装饰的选项 UploadedTemplatesService 。

I describe ObservableDataService inside 'providers' option of @Component decorator of UploadedTemplatesService.

但这个错误仍然发生。
为什么我仍然得到这个错误?

But this error still happens.Why i still getting this error?

重要:,现在在回购为prevent这个错误我使用 ObservableDataService 全球注射通过量引导(),但我不需要 ObservableDataService 为单例。

Important!: right now in repo for prevent this error i use global injection of ObservableDataService throug bootstrap(), but i does not need ObservableDataService as singleton.

我需要 UploadedTemplatesService 作为单身在新的 ObservableDataService 将注入。

I need UploadedTemplatesService as singleton in which new ObservableDataService will injected.

推荐答案

。使用 @Injectable 装饰就足够了。

IN fact when you implement a service you don't need to decorate it using the @Component decorator. Using the @Injectable decorator is enough.

我猜这是什么有点令人不安的是,有没有办法设置在 @Injectable 装饰供应商。这样做的原因是,提供者链接到组件。从组件中执行的所有处理将使用该组件的喷射器。

I guess that what is a bit disturbing is that there is no way to set providers on the @Injectable decorator. The reason for this is that providers are linked to components. All the processing executed from a component will use the injector of the component.

我的意思是,如果componentA调用serviceA调用serviceB。在 @Injectable 装饰器将尝试在componentA喷油器解决serviceB。

I mean if componentA calls serviceA that calls serviceB. The @Injectable decorator will try to resolve serviceB in the componentA injector.

这是很重要的另一部分是Angular2的分层喷射器功能。这里是所有这些元素的概述以及它们与我的样本关系:

Another part that is important is the "hierarchical injectors" feature of Angular2. Here is an overview of all these elements and their relations with my sample:

Application
(application injector)
     |
ComponentA  --- ServiceA --- ServiceB
(component injector)

在这样的应用,我们有两个喷油器:

In such application, we have two injectors:


  • 可以使用引导函数的第二个参数来配置应用程序注入

  • 可以使用提供商此组件的属性来配置的 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.

所以你的情况,你可以提供的组件或以上的范围内定义 ObservableDataService 。当然引导内定义它是最广泛的......

So in your case, you can define ObservableDataService within providers for the component or above. Sure defining it within bootstrap is the widest...

这个问题可以给你如何分层喷油器的更多详细信息
折角工作:

This question could give you more details about how hierarchical injectorsof Angular work:



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

这篇关于没有供应商ObservableDataService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 01:19