我已经在整个互联网上寻找了解决方案,但无济于事(不同情况等),因此请原谅下面的代码转储,我遇到的问题与循环依赖有关。代码转储用于提供上下文。
旁注:我对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
此类是所有组件的基础;
@ViewChildren
和ngAfterContentChecked
加载其子指令,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>
所有这些似乎都有效。我能够产生各种内容以及子级的深层嵌套等。我无法说出代码/实现的“正确性”,但是唯一的问题是循环依赖警告。
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 {
...
}