• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > 浅谈angular9中组件动态加载的实现方法

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

作者:站长图库 字体:[增加 减小] 来源:互联网 时间:2022-04-29

站长图库向大家介绍了angular9,组件动态加载等相关知识,希望对您有所帮助

按条件加载组件,实现组件的灵活切换,减少大量ngIf的使用,在angular中也是比较常见的操作。本篇文章就来大家一起交流一下angular组件的动态使用。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。


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

指令的创建

在添加组件之前,先要定义一个锚点来告诉 Angular 要把组件插入到什么地方。

在src/dynamic-banner/ad.directive.ts下

import { Directive, ViewContainerRef } from '@angular/core'; @Directive({    selector: '[ad-host]',})export class AdDirective {    constructor(public viewContainerRef: ViewContainerRef) { }}

AdDirective 注入了 ViewContainerRef 来获取对容器视图的访问权,这个容器就是那些动态加入的组件的宿主。

在 @Directive 装饰器中,要注意选择器的名称:ad-host,它就是你将应用到元素上的指令。


动态组件的核心代码

动态组件加载的html

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

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

动态组件的ts

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

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

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

import { Type } from '@angular/core';export interface AdComponent {    data: any;}

组件统一注册

src/dynamic-banner/share.module.ts

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

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中组件动态加载的实现方法

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2022-04-29Discuz3.4特殊字符乱码解决方案
  • 2022-04-29UEditor新增自定义按钮/UEditor增加自定义插件
  • 2022-04-29解决thinkphp5中图片处理中遇到的问题
  • 2022-04-29手把手教你使用ThinkPHP+phpExcel导入导出Excel数据
  • 2022-04-29完美解决更新和发布文章出现Unknown column 'weight
  • 2022-04-29DEDECMS用函数实现隔行换色以及分组加线
  • 2022-04-29CorelDraw实例教程:英文商标字体设计
  • 2022-04-29Thinkphp6如何利用ZipArchive打包下载文件
  • 2022-04-29详解Laravel前端工程化之mix
  • 2022-04-29Phpcms V9导航循环下拉菜单的调用技巧

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • uniapp中怎么实现直播旁路推流(步骤分享)
    • 关于 Laravel ORM 对 Model::find 方法进行缓存
    • uniapp适配到微信小程序需要注意些什么?
    • 解决微信video、audio 无法自动播放的问题
    • Photoshop绘制玻璃质感水晶按钮教程
    • PHP如何设置过期时间的session
    • 宝塔面板如何屏蔽禁止某个IP(IP段)访问
    • Photoshop制作金属质感的工具图标
    • 手把手教你在微信小程序中使用canvas绘制天气折线图(附代码)
    • PHP如何只抓取网页头

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有