我有 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
当我从 imports
子句中删除 ExchangeService
时(我留在 providers
子句中),异常消失了。我(目前)不明确需要 ExchnageService
在 imports
子句中(我不知道它有什么好处),我只想要 ExchangeService
可用于在全局组件中的其他服务中注入(inject)。
问题是 - 为什么不允许我在 imports
子句中写入 ExchangeService
? 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
关于javascript - 为什么我不能在@NgModule 中导入 Angular 2 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388811/