【问题标题】:Angular CLI production build, not known elementAngular CLI 生产版本,未知元素
【发布时间】:2017-06-15 15:48:50
【问题描述】:

在我的主要组件的 html 模板中,我使用来自已导入模块的组件的元素。当我尝试构建它时,我收到以下错误。

'df-dashboard-widget' is not a known element:
1. If 'df-dashboard-widget' is an Angular component, then verify that it is part of this module.
2. If 'df-dashboard-widget' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. 

还有这个错误:

ERROR in Template parse errors:
Can't bind to 'options' since it isn't a known property of 'df-dashboard-grid'.
1. If 'df-dashboard-grid' is an Angular component and it has 'options' input, then verify that it is part of this module.
2. If 'df-dashboard-grid' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. 

我的 HTML 模板如下所示:

<df-dashboard-grid [options]="options">
  <df-dashboard-widget [item]="item" *ngFor="let item of dashboard">
    <ng-container *ngComponentOutlet="getComponent(item.tag); injector: getProvider(item)"></ng-container>
  </df-dashboard-widget>
</df-dashboard-grid>

我已导入具有df-dashboard-grid 组件的模块,并且该组件已导出df-dashboard-widget 也会出现同样的问题。还有一个错误表明ngComponentOutlet 不是ng-container 的属性,尽管我已经从Angular 导入了CommonModule。我的主模块装饰器如下所示:

@NgModule({
  declarations: [
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    CommonModule,
    HttpModule,
    DashboardGridModule
  ].concat(Configuration.initialization.modules),
  providers: [],
  bootstrap: [DashboardComponent]
})

使用的带有组件装饰器的模块如下所示:

@NgModule({
  declarations: [
    DashboardGridComponent,
    DashboardWidgetComponent,
    DashboardGuideComponent,
    DashboardPreviewComponent
  ],
  imports: [
    CommonModule
  ],
  exports: [DashboardGridComponent, DashboardWidgetComponent],
})

此错误仅在生产模式下发生。在开发模式下,一切正常。

【问题讨论】:

  • 导出所有组件

标签: angular angular-cli


【解决方案1】:

我自己找到了这个问题的答案。显然它与此有关:

@NgModule({
  declarations: [
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    CommonModule,
    HttpModule,
    DashboardGridModule
  ].concat(Configuration.initialization.modules),
  providers: [],
  bootstrap: [DashboardComponent]
})

在我的配置文件中,我也使用了一组模块(由于某些原因)。在装饰器(或至少是主模块的装饰器)中,即使它返回有效的数据类型,也不允许在装饰器中拥有任何功能。

我通过添加另一个我称之为ImportModule 的模块解决了这个问题。

@NgModule({
  declarations: [],
  imports: Configuration.initialization.modules,
  exports: Configuration.entryComponents,
  providers: [],
  bootstrap: []
})

这对我有用,因为我有一个文件,其中所有使用的模块和组件都在一个单独的常量文件中。 Configuration 包含所有使用的模块的数组,entryComponents 是所有使用的组件的数组。在主模块中,我导入了这个模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-30
    • 2018-05-21
    • 1970-01-01
    • 2018-01-26
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多