【问题标题】:Error: Unexpected module 'DatePickerModule' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation错误:模块“AppModule”声明的意外模块“DatePickerModule”。请添加@Pipe/@Directive/@Component 注解
【发布时间】:2017-05-02 01:19:27
【问题描述】:

我想在我的应用程序中使用 Angular 的 DatePickerModule。所以我安装包如下: npm install ng2-datepicker-bootstrap --save

安装成功。所以现在,我在我的 AppModule 中导入了 DatePickerModule 模块,如下所示:

import { DatePickerModule } from 'ng2-datepicker-bootstrap';

@NgModule({

  declarations: [
    other Components......,
    DatePickerModule
  ],
  imports: [
    ........
  ],
  providers: [Service, DatePickerModule],
  bootstrap: [AppComponent]
})

export class AppModule { }

然后在我的info.component.html中使用下面的代码。

<datepicker [(ngModel)]="model.firstDate" [viewFormat]="'DD/MM/YYYY'" [modelFormat]="'YYYY-MM-DD'"  [id]="'firstDate'" [label]="'To'"></datepicker> 

但是当我运行这个项目时,我得到了这个错误: 错误:模块“AppModule”声明的意外模块“DatePickerModule”。请添加@Pipe/@Directive/@Component 注解。

顺便说一下,我是 Angular2 的新手。

【问题讨论】:

  • 添加到imports
  • 为什么在声明中添加它?它应该只在 import 中。阅读 npm 上的文档。
  • 操作,它应该在导入中。我现在就试试那个。
  • 试过了,但又出现了一个错误。无法绑定到“viewFormat”,因为它不是“datepicker”的已知属性。 ("
  • 改用attr.viewFormat

标签: angular bootstrap-datepicker


【解决方案1】:

为什么要将 DatePickerModule 放在提供程序中。当应用程序启动时,只需将服务放入其中以创建服务的实例。

【讨论】:

    【解决方案2】:

    检查 module.ts 文件中的 import 语句中是否缺少引用。 如果您的组件中有:

    import { DatePickerModule,SomeOtherModule } from 'somewhere';
    

    在 module.ts 文件中你必须有相同的导入

    import { DatePickerModule,SomeOtherModule } from 'somewhere';

    如果你只有

    import { DatePickerModule } from 'somewhere';

    或者你没有导入语句 那么这个错误将会被抛出,因为你没有在 import 语句中添加“SomeOtherModule”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 2021-08-15
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      相关资源
      最近更新 更多