DynamicComponentLoader 已被弃用。我正在尝试构建一个父组件,该父组件根据子组件数组在父组件内部呈现子组件,这意味着每个用户都有一个设置文件,其中包含需要在父组件中的子组件。现在 parent 看起来像这样:
import { Component, OnInit, Input, ComponentResolver, ViewContainerRef } from '@angular/core';
import { Child1Component } from '../children/child1.component'
import { Child2Component } from '../children/child2.component'
import { Child3Component } from '../children/child3.component'
import { ParentService } from './parent.service'
@Component({
selector: 'parent',
directives: [Child1Component, Child2Component, Child3Component],
providers: [ParentService],
template: `
<style>
.parent{
background-image: linear-gradient(141deg, #8ecb45 0%, #97cd76 71%, #96d885 100%);
width: 100%;
height: 200px;
}
</style>
<div class="parent"></div>
`
})
export class ParentComponent {
constructor(private parentService:ParentService, private children: any, viewContainer: ViewContainerRef, private componentResolver: ComponentResolver) {
this.children = parentService.getChildren();
for(var i = 0; i < children.length; i++) {
this.componentResolver.resolveComponent(children[i].component)
.then(componentFactory => {
const ctxInjector = viewContainer.injector;
return viewContainer.createComponent(componentFactory, 0, ctxInjector);
})
}
}
}
现在,在 ParentComponent 构造函数中调用服务似乎会导致一些问题。但在我着手循环 componentResolver 之前,我能够在父节点下附加一个组件,但不在其中。
有谁知道构建动态父组件的更好方法吗?这是为了管理布局的仪表板类型。大多数示例都明确说明将加载多少个组件。这对我不起作用。我看过一些关于如何使这项工作起作用的帖子,但到目前为止,我还没有看到任何带有配置文件的 RC 运行。
这最接近我正在尝试做的事情:http://plnkr.co/edit/jAmMZKz2VqponmFtPpes?p=preview 但它使用 dcl...
目前我收到一个错误,我在启动应用程序时没有弄明白:TypeError: Cannot read property 'query' of null
感谢您的帮助!
如果您想下载并试一试,这里有一个链接:
https://github.com/weswhite/ng2-layout
编辑/更新:这可能与 child 有关:我正在注入(inject)的任何 child 。我不认为我这样做是正确的,仍然在弄清楚这一点......
最佳答案
您可以将 ComponentResolver 与子 ViewContainerRef 一起使用以动态创建组件并将它们加载到父组件中。
@Component({
selector: 'parent',
providers: [ParentService],
template: `
<style>
.parent{
background-image: linear-gradient(141deg, #8ecb45 0%, #97cd76 71%, #96d885 100%);
width: 100%;
height: 200px;
}
</style>
<div #content></div>
`
})
export class ParentComponent {
@ViewChild('content', { read: ViewContainerRef }) contentContainer: ViewContainerRef;
children: Component[];
constructor(
private parentService:ParentService,
private resolver: ComponentResolver) {
this.children = parentService.getChildren();
}
ngOnInit() {
this.children.forEach( (component, index) => this.loadComponent(component, index));
}
private loadComponent(component: Component, index: number) {
return this
.resolver
.resolveComponent(component)
.then( factory =>
this.contentContainer.createComponent(factory, index, this.contentContainer.injector))
}
}
我还升级了提供的 plunker 以使用最新的 @angular 和 ComponentResolver 而不是 DynamicComponentLoader: 柱塞:http://plnkr.co/edit/Z6bXwBcwAnf4DSpNlU8H?p=preview
将 DCL 与 loadNextTolocation 结合使用:http://plnkr.co/edit/NYgJzz9UjrtGsNnO5WXS?p=info
P.S:你得到 TypeError: Cannot read property 'query' of null,因为你注入(inject) children
的方式,你必须指定一个可注入(inject)类型。
关于angularjs - Angular RC 2 - 从 json 文件动态渲染组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37368068/