angular 多组件同时动态展现

           有这么个需求,页面可以定制项目中的组件,不同的人显示的组件不一样,每个组件是个独立的页面,而不是通过路由跳转的单页面,也就是一个页面上显示多个动态子组件组成的页面。这时候,用angular的动态组件就可以解决问题,实现方案如下(部分代码直接拷贝angular中文文档):

 

一、创建指令

  src/app/ad.directive.ts


import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[ad-host]',
})
export class AdDirective {
  constructor(public viewContainerRef: ViewContainerRef) { }
}

二、加载组件

rc/app/ad-banner.component.ts (template)

template: `
            <div class="ad-banner-example">
              <h3>Advertisements</h3>
              <ng-template ad-host></ng-template>
            </div>
          `

三、解析组件(关键步骤)

rc/app/main.component.ts 

@ViewChild(WidgetDirective) widgetHost: WidgetDirective;


  userWPojos: any = [];
  ws: any = [];


  constructor(private wService: WService,
              private componentFactoryResolver: ComponentFactoryResolver) {
  }

  ngOnInit() {
    this.getData();
  }
  /**
  *获取当前用户所拥有的组件(页面)的方法(业务需求)
  */
  getData() {
    this.wService.getData().subscribe(
      data => {
        this.userWPojos = data['resultValue']['userWPojos'];
        for (const userW of this.userWPojos) {
          this.ws = [...this.ws, new WItem(userW.component, userW)];
        }
        this.loadComponent();
      });
  }


  loadComponent() {
    const viewContainerRef = this.widgetHost.viewContainerRef;
    viewContainerRef.clear();

    for (const w of this.ws) {
      // 通过componentMap将组件名字转换为组件Type类型
      const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentMap[w.component].prototype.constructor);
      const componentRef = viewContainerRef.createComponent(componentFactory);
      // 所有动态组件都要实现的接口 WComponent,WComponent只有一个data属性 
      (<WComponent>componentRef.instance).data = w.data;
    }

  }

 

四、其他代码

componentMap.ts

import {SComponent} from "../components/resource/s/s.component";
import {HComponent} from "../components/resource/h/h.component";
import {BComponent} from "../components/system/b/b.component";
/**
 * Created by ... on ...
 * 用于组件字符串名称转化为Type类
 */

export const componentMap = {
  SComponent: SComponent,
  HComponent: HComponent,
  BComponent: BComponent,
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值