浅谈angular9中组件动态加载的实现方法


当前第2页 返回上一页

相关推荐:《angular教程》

动态组件的核心代码

动态组件加载的html

src/dynamic-banner/ad-banner.component.html

1

2

3

4

<div class="ad-banner-example">

  <h3>Advertisements</h3>

  <ng-template ad-host></ng-template>

</div>

动态组件的ts

src/dynamic-banner/ad-banner.component.ts

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

import { Component, Input, OnInit, ViewChild, ComponentFactoryResolver, OnDestroy, SimpleChanges } from '@angular/core';

 

import { AdDirective } from './ad.directive';

import { AdItem }      from './ad-item';

import { AdComponent } from './ad.component';

import { componentMap } from './component/utils';

@Component({

  selector: 'app-ad-banner',

  templateUrl: './ad-banner.component.html',

  // styleUrls: ['./ad-banner.component.css']

})

export class AdBannerComponent implements OnInit {

  @Input() type: string = 'ad1' // 传入的key,确定加载那个组件

  @Input() data: any = {} // 传入组件的数据

  @ViewChild(AdDirective, {static: true}) adHost: AdDirective; // 动态组件的指令

  constructor(private componentFactoryResolver: ComponentFactoryResolver) { }

 

  ngOnInit() {

    this.loadComponent();

  }

  ngOnChanges(changes: SimpleChanges): void {

    if (changes['type']) this.loadComponent()

  }

 

  loadComponent() {

    // adItem 要加载的组件类,以及绑定到该组件上的任意数据

    const adItem = new AdItem(componentMap[this.type], this.data)

    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(adItem.component);

    const viewContainerRef = this.adHost.viewContainerRef;

    viewContainerRef.clear();

    const componentRef = viewContainerRef.createComponent(componentFactory);

    (<AdComponent>componentRef.instance).data = adItem.data;

  }

}

ad-item.ts

src/dynamic-banner/ad-item.ts

1

2

3

4

5

import { Type } from '@angular/core';

 

export class AdItem {

  constructor(public component: Type<any>, public data: any) {}

}

ad.component.ts

src/dynamic-banner/ad.component.ts

1

2

3

4

import { Type } from '@angular/core';

export interface AdComponent {

  data: any;

}

组件统一注册

src/dynamic-banner/share.module.ts

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import { NgModule } from '@angular/core';

import { CommonModule } from '@angular/common';

import { componets } from './component/utils';

import { AdDirective } from './ad.directive';

import { AdBannerComponent } from './ad-banner.component';

 

@NgModule({

  imports: [

    CommonModule

  ],

  exports:[

    [...componets],

    AdDirective,

    AdBannerComponent,

  ],

  declarations: [

    [...componets],

    AdDirective,

    AdBannerComponent,

  ],

  entryComponents: [

    [...componets]

  ]

})

export class ShareModule { }

组件的映射

src/dynamic-banner/component/utils.ts

1

2

3

4

5

6

7

8

9

10

11

import { HeroProfileComponent } from "./hero-profile.component";

import { HeroJobAdComponent } from './hero-job-ad.component';

const componentMap = {

    ad1: HeroProfileComponent,

    ad2: HeroJobAdComponent

}

const componets = [

    HeroProfileComponent,

    HeroJobAdComponent

]

export {componets, componentMap}

效果图

在这里插入图片描述

更多编程相关知识,请访问:编程视频!!

以上就是浅谈angular9中组件动态加载的实现方法的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

浅谈angular9中组件动态加载的实现方法

更多相关阅读请进入《angular9组件动态加载实现》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...