为什么我不能在@NgModule 中导入 Angular 2 服务?

汤姆

我有ExchangeServiceAngular 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 

当我ExchangeServiceimports子句中删除(并且我离开providers子句)时,异常消失我(目前)没有明确需要ExchnageServiceinimports子句(我不知道它有什么好处),我只想ExchangeService在全局组件中的其他服务中注入可用。

问题是 -为什么我不允许写ExchangeServiceimports条款中? 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章