2017-06-15 37 views
1

在我的主要組件的html模板中,我使用了我導入的模塊中的組件的元素。當我試圖建立這個時,我得到以下錯誤。Angular CLI生成版本,未知元素

'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部件內置模塊,這部分是導出的。同樣的問題發生在。還有一個錯誤說雖然我已經從Angular導入CommonModule,但ngComponentOutlet不是ng-container的屬性。我的主要模塊的裝飾看起來是這樣的:

@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], 
}) 

該錯誤僅發生在生產模式時。在開發模式下,一切工作正常。

+0

導出所有組件 – alehn96

回答

0

我自己找到了這個問題的答案。顯然它與此有關:

@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: [] 
}) 

這適用於我,因爲我有一個文件,所有使用的模塊和組件都在一個單獨的常量文件。配置有所有使用的模塊的數組,entryComponents是所有使用的組件的數組。在主模塊中,我導入該模塊。