我在互联网上寻找解决方案,但无济于事(不同情况等),所以请原谅下面的代码转储,我遇到的问题与循环依赖有关。代码转储是为了提供上下文。
旁注:我对 Angular 和 Typescript 相当陌生。
这个概念
当然,我正在尝试构建一组嵌套组件,它们都扩展基类以简化编码。这些组件可以包含彼此的任何子组件。为了加载子组件,基类使用一个指令来决定加载哪个组件来代替它。最好的例子就是想到嵌套<div>
and <section>
blocks.
这是我的代码:
该指令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
此类是所有组件的基础;
- Uses
@ViewChildren
and ngAfterContentChecked
加载其子指令,
- Sets
childNodes
when 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
请帮我摆脱它们......
SOLUTION
根据 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;
}
}
}
And div.ts
以及后续组件
@HtmlComponent({map: ['div']})
@Component({
selector: 'custom-div',
templateUrl: './div.html',
})
export class DivComponent extends Base {
.
.
.
}