angular2 - 没有DatepickerConfig的服务提供者

时间:2017-01-11 09:33:53

标签: angular ng2-bootstrap ng2-datepicker

我正在实施ng2-bootstrap datepicker,但收到以下错误。请帮帮我。

EXCEPTION: Uncaught (in promise): Error: Error in
         http://localhost:8888/app/components/report.component.html:3:0 caused by: No provider for DatepickerConfig! 

module.ts

 import { DatepickerModule } from 'ng2-bootstrap';

  @NgModule({
   imports: [DatepickerModule],
   declarations: [],
   providers: [],
   bootstrap: [AppComponent]
})

component.ts

@Component({
 module: module.id,
 selector:'my-date',
 template: `
  <h4>My Date Picker</h4>
  <datepicker></datepicker>`
})

 export class MyComponent {
 }

systemjs.config.js

System.config({ map: { 'ng2-bootstrap':
 'npm:ng2-bootstrap/bundles/ng2-bootstrap.umd.js'} })

1 个答案:

答案 0 :(得分:6)

你应该使用forRoot(),因为它是在样本库中以这种方式定义的。

  @NgModule({
   imports: [DatepickerModule.forRoot()],
   declarations: [],
   providers: [],
   bootstrap: [AppComponent]
})
  

按照惯例,forRoot静态方法既提供又配置服务。它接受一个服务配置对象并返回一个ModuleWithProviders,它是一个带有两个属性的简单对象:

     

•ngModule - CoreModule类

     

•提供商 - 配置的提供商

     

根AppModule导入CoreModule并将提供程序添加到AppModule提供程序。

来源:https://angular.io/docs/ts/latest/guide/ngmodule.html#!#configure-core-services-with-coremodule-forroot

示例存储库:https://github.com/valor-software/angular2-quickstart/blob/master/app/app.module.ts