【问题标题】:No component factory found. Did you add it to @NgModule.entryComponents?未找到组件工厂。你把它添加到@NgModule.entryComponents 了吗?
【发布时间】:2018-01-06 20:24:34
【问题描述】:

在使用带有 angular4 的 ag-grid 时,我遇到了以下错误消息。

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

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

但我的代码在错误消息下方。

ERROR 错误:未找到 RedComponentComponent 的组件工厂。做过 你把它添加到@NgModule.entryComponents 吗?

这是我的应用模块代码。我已经设置为 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

我的问题。为什么这段代码无法解析这个组件。

【问题讨论】:

标签: angular ag-grid


【解决方案1】:

您在从服务器加载数据时将string 传递给`ComponentFactoryResolver,但它应该是组件类型。

header.json

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

为了解决这个问题,我会创建入口组件映射,例如:

entry-components.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];
       }
     });

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 2019-11-05
    • 2019-12-06
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多