Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我无法在@NgModule中导入Angular 2服务?_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 为什么我无法在@NgModule中导入Angular 2服务?

Javascript 为什么我无法在@NgModule中导入Angular 2服务?,javascript,angular,typescript,Javascript,Angular,Typescript,我有ExchangeServiceAngular 2@Injectable类,我有一个应用程序主模块: @NgModule({ imports: [ BrowserModule, HttpModule, FormsModule, ReactiveFormsModule, ExchangeService, RouterModule.forRoot(routes) ], provid

我有
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
子句(我不知道它有什么好处),我只想
ExchnageService
可以在全球组件的其他服务中注入


问题是-为什么不允许我在
imports
子句中编写
exchangeseservice
imports
子句包含其他类型脚本类,如
HttpModule
等等,为什么
imports
子句也不允许包含
exchangeseservice

应该在提供者内部导入

除去

 imports: [
        BrowserModule,
        HttpModule,
        FormsModule,
        ReactiveFormsModule,       
        RouterModule.forRoot(routes)
    ]
导入:用于导入支持模块,如FormsModule、RouterModule、CommonModule或任何其他定制功能模块

读一下这个

  • 使用
    imports
    可以在当前模块中提供其他(FormsModule、HttpModule等)模块
  • 使用
    提供者
    可以注入组件、其他服务、管道等所需的服务

导入
用于导入当前模块中的其他角度模块

提供程序
用于告诉angular哪些类型(实际上是实例)可用于依赖项注入

角度模块是组件、指令、管道、服务等的集合。它是一种抽象,有助于构建更大的项目,并包括其他地方编写的功能

每个应用程序至少包含一个模块(您的案例中的主模块)。但是,当应用程序增长时,你会被诱惑分成几个模块

您的
ExchangeService
可能不是一个模块,而是一个服务(一个包含一些方法的类)。应用程序的其他组件和服务可能取决于ExchangeService的具体实例,如果将ExchangeService添加到providers数组,Angular将创建它的实例,并在创建(例如)组件时将其作为依赖项注入(假设您将ExchangedService声明为组件构造函数的参数)


我建议阅读。根据我的2美分,对这些体系结构构建块的理解是成功使用Angular所必需的。

我明白了这一点,但我的问题是-service ExchangeService和module BrowserModule之间的区别是什么-为什么我应该导入一个而不导入其他?服务和模块是双向的不同things@TomR有什么理由删除答案吗?非常感谢您的回答和澄清,但其他答案提供了更多的理论,因此我被要求将其标记为已接受。@TomR没关系!我已将链接添加到描述中,因为我不必重复相同的内容