动态渲染组件

Luc*_*rto 7 dashboard typescript gridster angular

我有这个数组:

this.dashboard = [
     {name: '<app-incassi-provvigioni></app-incassi-provvigioni>'},
     {name: '<app-scheda-punti-vendita></app-scheda-punti-vendita>'}
]
Run Code Online (Sandbox Code Playgroud)

我在 ngOnInit 循环中填充这个数组。我想知道当我在 html 中读取数组时如何呈现组件:

<gridster [options]="gridsterOptions">
    <gridster-item [item]="item" *ngFor="let item of dashboard">
        <!-- your content here -->
        {{item.name}}
    </gridster-item>
</gridster>
Run Code Online (Sandbox Code Playgroud)

当然,现在它返回对象中包含的字符串,但我正在尝试找到渲染组件的解决方案。有可能这样做吗?

更多细节:我正在开发一个仪表板类型的应用程序,我从数据库中检索用户仪表板的列表,一旦主应用程序组件准备就绪,我就会尝试动态呈现这些组件。使用 Angular 7 和 Gridster2。

Nec*_*hiK 9

您可以动态注入组件

只需使用 @Input() 组件创建组件;

export class DynamicContentComponent implements OnInit {

  @Input() component: any;
  @Input() data: any;

  constructor(public viewContainerRef: ViewContainerRef,
              private componentFactoryResolver: ComponentFactoryResolver) { }

  ngOnInit() {
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.component);
    this.viewContainerRef.clear();

    const componentRef = this.viewContainerRef.createComponent(componentFactory);
    (<DynamicComponentRef>componentRef.instance).data = this.data;
  }
}
Run Code Online (Sandbox Code Playgroud)

并在 HTML 中使用它

<app-dynamic-content [component]="someComponent" [data]="data"></app-dynamic-content>
Run Code Online (Sandbox Code Playgroud)

数据示例

someComponent = SchedaPuntiVendita;
Run Code Online (Sandbox Code Playgroud)

动态组件应该添加到entryComponents您的module

您也可以创建某种类型factory,它将接收一些字符串并依赖于它返回您的组件类

工厂示例

@Injectable()
export class DynamicContentComponentFactory {

  constructor() { }

  get(type: ContentType): any {
    switch (type) {
      case 'scheda':
        return SchedaPuntiVendita;
    }
  }

Run Code Online (Sandbox Code Playgroud)

DynamicContentComponent稍微修改一下

@Input() contentType: string;

constructor(..., private factory: DynamicContentComponentFactory) { }
...

const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.factory.get(this.contentType));

...
Run Code Online (Sandbox Code Playgroud)

  • 不再需要 ComponentFactoryResolver。Angular 团队对其进行了简化,因此您可以直接创建组件,如下所示:`this.viewContainerRef.createComponent(this.component);` (3认同)

ind*_*oft 7

而不是传递组件标签名称(在您的情况下为“app-incassi-provvigioni”),而是传递组件名称(TestComponenet),然后从您的视图中调用该函数并使用指令将其动态渲染。

例如:

<gridster [options]="gridsterOptions">
  <gridster-item [item]="item" *ngFor="let item of dashboard">
    <div class="inner">
      <ng-template dynamic-template> </ng-template>
    </div>
  </gridster-item>
</gridster>
Run Code Online (Sandbox Code Playgroud)

动态模板是一个指令,帮助我们加载组件。

  • @LucaAlberto,我刚刚做了示例代码检查链接:[链接](https://stackblitz.com/edit/angular-yz8w1u)。如果这适合您,请标记为答案。谢谢 (4认同)

小智 3

也许您可以使用角度开关来实现此目的:

<div [ngSwitch]="item.name">
    <app-incassi-provvigioni *ngSwitchCase="incassi"></app-incassi-provvigioni>
    <app-scheda-punti-vendita *ngSwitchCase="scheda"></app-scheda-punti-vendita>
</div>
Run Code Online (Sandbox Code Playgroud)