代码之家  ›  专栏  ›  技术社区  ›  sungyong

未找到组件工厂。你把它添加到@NgModule.entryComponents了吗?

  •  7
  • sungyong  · 技术社区  · 9 年前

    我试图在通过HTTP获取json后显示行数据。

    this.gridOptions.api.setRowData(this.gridOptions.rowData);
    

    但我的代码出现以下错误消息。

    错误:未找到RedComponentComponent的组件工厂。做

    这是我的应用程序模块代码。我已经设置为entryComponents。

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    import { HttpModule } from '@angular/http';
    import { AgGridModule} from "ag-grid-angular/main";
    
    import { AppComponent } from './app.component';
    import { MyGridApplicationComponent } from './my-grid-application/my-grid-application.component';
    import { RedComponentComponent } from './red-component/red-component.component';
    
    @NgModule({
      imports: [
        BrowserModule,
        AgGridModule.withComponents(
          [RedComponentComponent]
        ),
        FormsModule,
        HttpModule
      ],
      declarations: [
        AppComponent,
        MyGridApplicationComponent,
        RedComponentComponent
      ],
      entryComponents: [
        RedComponentComponent
      ],  
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    error position of my code

    1 回复  |  直到 9 年前
        1
  •  7
  •   yurzui    9 年前

    你正在通过 string

    header.json

    {
      "headerName": "DetailCode",
      "field": "detailCode",
      "cellRendererFramework": "RedComponentComponent", // string
      "width":100
    },
    

    为了解决这个问题,我将创建条目组件映射,如:

    入口组件.ts

    import { RedComponentComponent } from './red-component/red-component.component';
    
    export const entryComponentsMap = {
      'RedComponentComponent': RedComponentComponent
    };
    

    然后将加载从json字符串映射到组件类

    comm-code-grid-option.service.ts

    private fetchColumnDefs() {
     console.log("fetchColumnDefs()");
    
     this.http.get(this.API_URI + "resident/header.json").subscribe(
       r => {
         this.gridOptions.columnDefs = r.json();
         this.gridOptions.columnDefs.forEach((x: any) => {
           if (x.cellRendererFramework) {
             x.cellRendererFramework = entryComponentsMap[x.cellRendererFramework];
           }
         });