如何编写一个 Angular 2 依赖于其他服务并且组件只注入父级的服务?
How to write an Angular 2 service that depends on other services and have components only inject the parent?
如果我有一个依赖于 ChildService 的 ParentService,您必须在 @Component 定义的 "providers" 属性中列出 ParentService 和 ChildService。
有没有办法实现ParentService,让它自动注入ChildService,让组件只需要引用ParentService?
事实上,服务需要从执行调用的组件的注入器中可见。注入器仅链接到组件而不是服务,因此无法在此级别进行配置。
通过在引导您的应用程序时指定服务,您将不会遇到此问题:
bootstrap(AppComponent, [ ParentService, ChildService ]);
您也可能对这个问题感兴趣:
- What's the best way to inject one service into another in angular 2 (Beta)?
如果我有一个依赖于 ChildService 的 ParentService,您必须在 @Component 定义的 "providers" 属性中列出 ParentService 和 ChildService。
有没有办法实现ParentService,让它自动注入ChildService,让组件只需要引用ParentService?
事实上,服务需要从执行调用的组件的注入器中可见。注入器仅链接到组件而不是服务,因此无法在此级别进行配置。
通过在引导您的应用程序时指定服务,您将不会遇到此问题:
bootstrap(AppComponent, [ ParentService, ChildService ]);
您也可能对这个问题感兴趣:
- What's the best way to inject one service into another in angular 2 (Beta)?