【发布时间】:2017-10-12 00:09:46
【问题描述】:
我的项目设置如下:
- 应用程序
- app.module (
imports: []) - 共享
- shared.module (
declarations: [sharedComponent], providers: [sharedService]) - 共享服务
- 共享组件
- shared.module (
- 模块1
- module1.module (
imports: [sharedModule]) - module1.component(路由子使用sharedComponent)
- module1.module (
- 模块2
- module2.module (
imports: [sharedModule]) - module2.component(路由子使用sharedComponent)
- module2.module (
- app.module (
这不会引发任何错误,但在 sharedService 构造函数中设置断点表明它不是单例。
我可以将我的导入改为:
- 应用程序
- app.module (
imports: [sharedModule]) - 共享
- shared.module (
declarations: [sharedComponent], providers: [sharedService]) - 共享服务
- 共享组件
- shared.module (
- 模块1
- module1.module (
imports: []) - module1.component(路由子使用sharedComponent)
- module1.module (
- 模块2
- module2.module (
imports: []) - module2.component(路由子使用sharedComponent)
- module2.module (
- app.module (
sharedService 现在似乎是单例,但我收到错误消息
组件 sharedComponent 不是任何 NgModule 的一部分,或者该模块尚未导入您的模块中
我希望我的共享服务是单例的,但需要访问共享组件。我做错了什么?
【问题讨论】:
-
我认为您需要将
shared设为provider而不是module。 -
你能告诉我们你的
sharedModule吗?
标签: angular angular2-routing angular2-services