gpt4 book ai didi

angular - 从 json 响应创建动态嵌套组件

转载 作者:搜寻专家 更新时间:2023-10-30 21:45:21 25 4
gpt4 key购买 nike

我正在尝试创建基于 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 需要先渲染。

它作为一个数组对象嵌入,我想在这个数组中呈现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

仅基于它我创建了我的项目。但无法使其正常工作。

请帮忙。

最佳答案

执行此操作的最简单方法可能如下所示:

@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

关于angular - 从 json 响应创建动态嵌套组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com