具有动态嵌套组件的角圆相关性

时间:2020-06-25 08:11:33

标签: angular typescript circular-dependency

我已经在整个互联网上寻找了解决方案,但无济于事(不同情况等),因此请原谅下面的代码转储,我遇到的问题与循环依赖有关。代码转储用于提供上下文。

旁注:我对Angular和Typescript还是很陌生。

概念

我正在尝试构建一组嵌套组件,它们都扩展了基类以简化编码。这些组件可以包含彼此的子代。要加载子组件,基类使用指令,该指令需要确定要在其位置加载的组件。最好的例子是考虑嵌套<div><section>块。

这是我的代码:

指令directive.ts

在调用load()时加载其各自的组件

import { ComponentFactoryResolver, Directive, ViewContainerRef } from '@angular/core';

import { DivComponent } from './div'; // < -- Part of the dependency issues
import { SectionComponent } from './section'; // < -- Part of the dependency issues

@Directive({
    selector: '[child-node]',
})
export class ChildNodeDirective {

    @Input() type: string;
    @Input() content: any;

    constructor(
        private container: ViewContainerRef,
        private resolver: ComponentFactoryResolver,
    ){}

    load(): void {

        let component: Type<any>;

        switch (this.type) {
            case 'div' : component = DivComponent; break;
            case 'section' : component = SectionComponent; break;
        }

        if (component) {
            const factory = this.resolver.resolveComponentFactory(component);
            const componentRef = this.container.createComponent(factory);
            componentRef.instance.content = this.content;
        }

    }

}
基类base.ts

此类是所有组件的基础;

  1. 使用@ViewChildrenngAfterContentChecked加载其子指令,
  2. 在调用childNodes时设置set content,以便组件可以呈现其<child-node>元素
import { AfterContentChecked, QueryList, ViewChildren } from '@angular/core';

import { ChildNodeDirective } from './directive'; // < -- Part of the dependency issues

export abstract class Base implements AfterContentChecked {

    // These are elements that the template will render into the directive
    public childNodes = {[type: string]: any} = {};

    private childrenLoaded = false;

    @ViewChildren(ChildNodeDirective) protected children: QueryList<any>;

    ngAfterContentChecked(): void {
        if (!this.childrenLoaded && this.children) {
            this.children.forEach((child: ChildNodeDirective) => {
                child.load();
            });
            this.childrenLoaded = true;
        }
    }

    set content(content: any) {
        this.childNodes = content.childNodes;
    }

}
div组件div.ts

此组件扩展了基础并仅呈现其子节点

import { Component } from '@angular/core';
import { Base } from './base'; // < -- Part of the dependency issues

@Component({
    selector: 'custom-div',
    templateUrl: './div.html',
})
export class DivComponent extends Base {

    public textContent: string;

    set content(content: any) {

        super.content = content;

        this.textContent = content.text;

    }

}
div模板div.html

<div>{{ textContent }}</div>

<div *ngFor="let child of childNodes">
  <ng-template child-node [content]="child.content" [type]="child.type"></ng-template>
</div>


TL; DR问题

所有这些似乎都有效。我能够产生各种内容以及子级的深层嵌套等。我无法说出代码/实现的“正确性”,但是唯一的问题是循环依赖警告。

WARNING in Circular dependency detected:
div.ts -> base.ts -> directive.ts -> div.ts

WARNING in Circular dependency detected:
section.ts -> base.ts -> directive.ts -> section.ts

请帮助我摆脱它们...

解决方案

根据Kai的最新建议,我创建了一个装饰器来收集元数据以在指令中使用。

directive.ts

中的更改

export const HtmlElementMap: {component: Type<any>, map: string[]}[] = [];

export function HtmlComponent(config: {map: string[]}) {
    return (target: Type<any>) => {
        ElementMap.push({component: target, map: config.map});
    };
}

@Directive({
    selector: '[child-node]',
})
export class ChildNodeDirective {

    ...

    load(): void {

        let component: Type<any>;

        HtmlElementMap
          .filter(v => v.map.indexOf(this.type) > -1)
          .forEach(
              v => {
                  if (undefined === component) {
                      component = v.component;
                  }
              }
          );

        if (component) {
            const factory = this.resolver.resolveComponentFactory(component);
            const componentRef = this.container.createComponent(factory);
            componentRef.instance.content = this.content;
        }

    }

}

div.ts以及后续组件


@HtmlComponent({map: ['div']})
@Component({
    selector: 'custom-div',
    templateUrl: './div.html',
})
export class DivComponent extends Base {

    ...

}

1 个答案:

答案 0 :(得分:2)

我认为图表中的红线是这里的问题。因此,如果在运行时注册组件(需要switch语句),则可以尝试解决并消除此依赖性。例如,Base类可以收集所有可用的组件,并将它们传递给ChildNodeDirective类。或者,您可以使用服务进行注册。如果您有一个有效的stackblitz示例,我们可以为您提供代码。

enter image description here