依赖注入不适用于Angular 2入门应用程序

时间:2017-01-13 15:37:16

标签: javascript angularjs angular typescript

当我尝试注入任何依赖时,我得到一个奇怪的错误

TypeError: Cannot set property 'stack' of undefined
at NoProviderError.set [as stack] (errors.js:64)
at assignAll (zone.js:704)
at NoProviderError.ZoneAwareError (zone.js:775)
at NoProviderError.BaseError [as constructor] (errors.js:26)
at NoProviderError.AbstractProviderError [as constructor] (reflective_errors.js:53)
at new NoProviderError (reflective_errors.js:102)
at ReflectiveInjector_._throwOrNull (reflective_injector.js:994)
at ReflectiveInjector_._getByKeyDefault (reflective_injector.js:1031)
at ReflectiveInjector_._getByKey (reflective_injector.js:981)
at ReflectiveInjector_.get (reflective_injector.js:743)
at AppModuleInjector.NgModuleInjector.get (ng_module_factory.js:149)
at CompiledTemplate.proxyViewClass.AppView.injectorGet (view.js:152)
at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (view.js:580)
at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (host.ngfactory.js:15)
at CompiledTemplate.proxyViewClass.AppView.createHostView (view.js:108)

我刚刚使用了Angular-Cli ng new PROJECT_NAME_HERE命令。

我的app.component.ts里面只有

import { Component } from '@angular/core';
import { FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass']
})
export class AppComponent {

 constructor(
   private formBuilder: FormBuilder
 ) {
 }

}

编辑: 我收到的错误是因为zone.js的v0.7.5存在问题, 和用户3492940& Santanu Biswas表示我还需要将ReactiveFormsModule导入我的app.module

3 个答案:

答案 0 :(得分:2)

您需要从ReactiveFormsModule中的@angular/forms导入AppModule,并将其包含在imports列表中。

答案 1 :(得分:1)

将zone.js降级为0.7.4

npm install --save zone.js@0.7.4

0.7.5版本中的问题。 Issue #595

答案 2 :(得分:0)

我对Angular 2组件仍然比较新,但是看看你的组件,我认为你需要moduleId:module.id,

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass']
})