Angular 动态嵌套组件的循环依赖
Angular Circular Dependencies with Dynamic Nested Components
我在整个互联网上寻找解决方案(不同情况等)无济于事,所以请原谅下面的代码转储,我遇到的问题与循环依赖有关。代码转储是为了提供上下文。
旁注:我对 Angular 和 Typescript 还很陌生。
概念
当然,我正在尝试构建一组嵌套组件,它们都扩展了一个基础 class 以简化编码。这些组件可以包含彼此的任何子组件。为了加载子组件,基础 class 使用一个指令来决定加载哪个组件。最好的例子是考虑嵌套 <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;
}
}
}
基地classbase.ts
这个 class 是所有组件的基础;
- 使用
@ViewChildren
和 ngAfterContentChecked
加载其子指令,
- 在调用
set content
时设置 childNodes
,以便组件可以呈现其 <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 {
.
.
.
}
在我看来,我图表中的红线就是问题所在。因此,如果您在运行时注册您的组件(需要 switch 语句),您可以尝试解决并删除这种依赖关系。例如 Base
class 可以收集所有可用的组件并将它们传递给 ChildNodeDirective
class。或者您可以使用服务来注册它们。如果您有可用的 stackblitz 示例,我们可以帮助您编写代码。
我在整个互联网上寻找解决方案(不同情况等)无济于事,所以请原谅下面的代码转储,我遇到的问题与循环依赖有关。代码转储是为了提供上下文。
旁注:我对 Angular 和 Typescript 还很陌生。
概念
当然,我正在尝试构建一组嵌套组件,它们都扩展了一个基础 class 以简化编码。这些组件可以包含彼此的任何子组件。为了加载子组件,基础 class 使用一个指令来决定加载哪个组件。最好的例子是考虑嵌套 <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;
}
}
}
基地classbase.ts
这个 class 是所有组件的基础;
- 使用
@ViewChildren
和ngAfterContentChecked
加载其子指令, - 在调用
set content
时设置childNodes
,以便组件可以呈现其<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 {
.
.
.
}
在我看来,我图表中的红线就是问题所在。因此,如果您在运行时注册您的组件(需要 switch 语句),您可以尝试解决并删除这种依赖关系。例如 Base
class 可以收集所有可用的组件并将它们传递给 ChildNodeDirective
class。或者您可以使用服务来注册它们。如果您有可用的 stackblitz 示例,我们可以帮助您编写代码。