我想将该providedIn
选项引用到提供可声明和依赖项的 Angular 模块。我创建了一个附加模块SharedModule
作为惰性模块的子模块,LazyModule
以便它可以用作我想要提供的惰性服务的“锚”(CounterService
在我的例子中)。我一直在遵循本指南来实现它,但显然做错了什么。另外,我做了一个小计划,但请查看StackBlitz以查看完整示例。
Eager Part Lazy Loaded Module
+----------------------------------------------+ +--------------------------------------+
|Routes: | | @NgModule({ |
| | | imports: [SharedModule] |
|{ | | } |
| path: 'lazy', | | export class LazyModule {} |
| loadChildren: './lazy/lazy.module#LazyModule| | |
|} | | |
+----------------------------------------------+ | SharedModule |
| +----------------------------------+ |
| |@NgModule({ | |
| | declarations: [SharedComponent],| |
| | exports: [SharedComponent] | |
| |}) | |
| |export class SharedModule {} | |
| +----------------------------------+ |
| |
| |
| LazyService |
| +----------------------------------+ |
| |@Injectable({ | |
| | providedIn: SharedModule | |
| |}) | |
| |export class CounterService { | |
| | counter = 0; | |
| |} | |
| +----------------------------------+ |
| |
+--------------------------------------+