我似乎明白,如果我在FeatureModule
中提供服务然后在我的AppModule
(根模块)中导入该模块,则功能模块中列出的所有提供程序都可以在应用程序范围内使用
然而,这段代码似乎不起作用。谁能帮我理解我做错了什么?
功能
@NgModule({
imports: [ CommonModule ],
declarations: [],
providers: [
DatetimeService, RandomService
]
})
export class FeaturesModule { }
根模块
@NgModule({
declarations: [ AppComponent, ChildComponent ],
imports: [
BrowserModule,
FormsModule,
FeaturesModule
],
bootstrap: [AppComponent]
})
export class AppModule { }
当我尝试在RandomService
中注入AppComponent
时,它会失败地失败
Error: StaticInjectorError[RandomService]:
StaticInjectorError[RandomService]:
NullInjectorError: No provider for RandomService!
at _NullInjector.get (core.js:923)
at resolveToken (core.js:1211)
at tryResolveToken (core.js:1153)
at StaticInjector.get (core.js:1024)
at resolveToken (core.js:1211)
at tryResolveToken (core.js:1153)
at StaticInjector.get (core.js:1024)
at resolveNgModuleDep (core.js:10584)
at NgModuleRef_.get (core.js:11805)
at resolveDep (core.js:12301)
我正在使用最新的Angular 5。