我有ExchangeService
角2 @Injectable
類和我有一個應用程序主模塊:爲什麼我不能在@NgModule中導入Angular 2服務?
@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
當我從imports
子句除去ExchangeService
(我留在異常消失providers
子句)。我沒有(目前)明確需要ExchnageService
在imports
條款(我不知道它有什麼好處),我只想ExchangeService
可用於全局組件中的其他服務的注入。
問題是 - 爲什麼我不允許在imports
條款中寫ExchangeService
條款?Imports
子句也包含其他TypeScript類 - 如HttpModule
等等以及爲什麼imports
子句不允許包含ExchangeService
?
我想通了,但我的問題是 - 服務ExchangeService和模塊BrowserModule之間的區別是什麼 - 爲什麼我應該導入一個,而不是導入其他? – TomR
服務和模塊是兩個不同的東西 – Sajeetharan
@TomR刪除答案的任何理由? – Sajeetharan