根据json响应创建动态嵌套组件

时间:2019-05-23 18:58:25

标签: angular typescript angular-dynamic-components

我正在尝试根据json响应创建嵌套的动态组件。

public content={type:'paragraph',depth:1,text:'Root',entityRanges:[{type:'LINK',offset:83,length:16,data:{target:'_self',url:'/index.htm'}}],embbeded:[{type:'text',text:'This is Text component'}]}

因此在上述结构中,其类型为paragraph,因此ParagraphComponent首先需要呈现。

它作为数组对象embbeded,在此数组中,我想渲染TextComponent

所以最终输出应该是这样的

<paraComp><p><textComp>This is Text component</textComp></p></paraComp>

下面是我尝试过的

主要组件

export class AppComponent implements OnInit {
  @ViewChild('container', { read: ViewContainerRef })
  public target: ViewContainerRef;
  public content = { type: 'paragraph', depth: 1, text: 'Root', entityRanges: [{ type: 'LINK', offset: 83, length: 16, data: { target: '_self', url: '/index.htm' } }], embbeded: [{ type: 'text', text: 'This is Text component' }] }

  constructor(private createDynamicComponentService: CreateDynamicComponentService) { }
  ngOnInit() {
    this.createDynamicComponentService.createComponent(this.content, this.target);
  }
}

主要HTML

<ng-container #container></ng-container>

createDynamicComponentService

export class CreateDynamicComponentService {
  private rootViewContainer: ViewContainerRef;
  private componentFactory: ComponentFactory<any>;
  private componentReference;
  constructor(
    private componentFactoryResolver: ComponentFactoryResolver,
    @Inject(CONTENT_MAPPINGS) private contentMappings: any
  ) { }

  setRootViewContainerRef(view: ViewContainerRef): void {
    this.rootViewContainer = view;
  }

  createComponent(content: any, container: ViewContainerRef) {
    const type = this.contentMappings[content.type];
    console.log(type);
    this.componentFactory = this.componentFactoryResolver.resolveComponentFactory(type);
    this.componentReference = this.rootViewContainer.createComponent(this.componentFactory);
    this.componentReference.instance.contentOnCreate(content);
  }
}

链接到项目https://stackblitz.com/edit/angular-dynamic-new?file=src/app/app.component.ts

我从这个https://github.com/sparkles-dev/ng-content-driven-angular/blob/master/src/app/reactive-content/content-host/content-host.component.ts

中引用了

仅以此为基础创建了我的项目。但是无法使其正常工作。

请帮助。

1 个答案:

答案 0 :(得分:1)

最简单的方法如下:

@Injectable()
export class CreateDynamicComponentService {
  constructor(
    private componentFactoryResolver: ComponentFactoryResolver,
    @Inject(CONTENT_MAPPINGS) private contentMappings: any
  ) { }

  createComponent(content: any, container: ViewContainerRef) {
    const type = this.contentMappings[content.type];

    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(type);
    const componentRef = container.createComponent<any>(componentFactory);

    if (componentRef.instance.contentOnCreate) {
      componentRef.instance.contentOnCreate(content);
    }

    if (!content.embedded) return;

    // render children recursively
    for (const embeddedContent of content.embedded) {
      // in order to render children component must define ViewContainerRef
      if (!componentRef.instance.embeddedContainer) {
        const cmpName = componentRef.instance.constructor.name;
        throw new TypeError(`Trying to render embedded content. 
          ${cmpName} must have @ViewChild() embeddedContainer defined`);
      }

      this.createComponent(embeddedContent, componentRef.instance.embeddedContainer);
    }
  }

}

为了渲染子组件,组件应定义将要渲染子组件的容器,例如:

paragraph.component.html

<p>
  <ng-container #embeddedContainer></ng-container>
</p>

paragraph.component.ts

export class ParagraphComponent implements OnInit {
  @ViewChild('embeddedContainer', { read: ViewContainerRef }) embeddedContainer: ViewContainerRef;

Forked Stackblitz