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

标签 angular ag-grid

在将 ag-grid 与 angular4 一起使用时,我遇到了以下错误消息。

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

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

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

ERROR Error: No component factory found for RedComponentComponent. Did you add it to @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

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

最佳答案

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

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

关于angular - 未找到组件工厂。你把它添加到@NgModule.entryComponents 了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45412915/

相关文章:

Angular 4 - 没有将 "exportAs"设置为 "ngForm"的指令

javascript - 如何使用 Angular 8 为某些回调(Leaflet 库)设置事件处理程序

javascript - 禁用 Ag-grid 中的复选框选择

html - Angular 4 Forms --> 从输入中搜索结果

c# - Asp.net 核心 webapi 获取从 Angular4 应用程序发布的空值

javascript - 如果属性值匹配,则从数组中删除对象

typescript - 如何在 angular2 中将 ag-grid 与 Observable 一起使用?

javascript - ag-Grid javascript无法调用columnApi中的方法

javascript - 在 Angular 2 的 ag-grid 中使用 gridOptions 中的变量

javascript - ag-Grid 国际化所有可能的属性