angular 2 中没有 HttpService 提供程序

No Provider for HttpService in angular 2

我在 angular 2 / ionic 2 项目中得到了关注 "constellation"。

Component1 -> Service1 -> Service3
Component2 -> Service2 -> Service3

(其中 -> 表示类似 "using" 或 "calling")

没有在组件的提供程序数组中注入 Service3,我总是遇到错误

No Provider for Service3

有没有一种方法可以将服务注入到 angular2 中的服务中,而不必将它们全部注入到组件中?分别如何管理依赖项而不必在组件中显式列出所有依赖项?

import {Service1} from  '../../../providers/service1';
import {Service2} from  '../../../providers/Service3';

@Component({
    templateUrl: '...',
    providers: [Service1, Service3]}) // <= here I would like to not list Service2

export class Component1 {
}

我尝试按照Inject services in another service angular 2中的描述在服务中注入服务,但没有成功

当然,只要我的项目保持小规模,在组件中列出所有服务依赖项就没有什么大不了的,但这并不是真正可扩展的。

@rinukkusu 解决了这个问题。他建议在应用程序的根目录中注入所有这些 "shared" 服务(如我的示例中的 Service3),而不是在许多地方多次注入服务。

然后我仍然需要列出所有服务依赖项,也就是至少在组件中包含一次服务,但至少我不必复制我的代码。

所以在app.ts

import {Service2} from  '../../../providers/service2';

@Component({
templateUrl: 'build/app.html',
providers: [Service2]
})

例如服务(如我示例中的 Service1)

import {Service2} from  './service2';

@Injectable()
export class Service1 {

constructor(private service2:Service2) {

}

注入在组件内部分层工作,因此如果您希望在同一层次结构级别使用服务,则需要在该级别之上提供该服务。在您的情况下,您需要在应用程序 bootstrap.

中提供此 Service3