我有ExchangeService
Angular 2@Injectable
类,我有一个应用程序主模块:
@NgModule({
imports: [
BrowserModule,
HttpModule,
FormsModule,
ReactiveFormsModule,
ExchangeService,
RouterModule.forRoot(routes)
],
providers: [
{ provide: LocationStrategy,
useClass: HashLocationStrategy
},
ExchangeService
]
...})
这引发了异常
(index):16 Error: (SystemJS) Unexpected value 'ExchangeService' imported by the module 'Application5'
Error: Unexpected value 'ExchangeService' imported by the module 'Application5'
Error: (SystemJS) Unexpected value imported by the module Error: Unexpected value imported by the module
Error: Unexpected value imported by the module at eval (http://127.0.0.1:8080/node_modules/@angular/compiler/bundles/compiler.umd.js:13982:37) at Array.forEach (native) at CompileMetadataResolver.getNgModuleMetadata
Error: Unexpected value imported by the module at eval at Array.forEach (native) at CompileMetadataResolver.getNgModuleMetadata
当我ExchangeService
从imports
子句中删除(并且我离开providers
子句)时,异常消失。我(目前)没有明确需要ExchnageService
inimports
子句(我不知道它有什么好处),我只想ExchangeService
在全局组件中的其他服务中注入可用。
问题是 -为什么我不允许写ExchangeService
在imports
条款中? Imports
子句也包含其他 TypeScript 类——比如HttpModule
等等,为什么imports
子句也不允许包含ExchangeService
?
您应该导入内部提供者
从......中去除
imports: [
BrowserModule,
HttpModule,
FormsModule,
ReactiveFormsModule,
RouterModule.forRoot(routes)
]
导入:用于导入支持模块,如 FormsModule、RouterModule、CommonModule 或任何其他自定义功能模块。
读这个 what-is-difference-between-declarations-providers-and-import-in-ngmodule
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句