当前位置: 首页 > news >正文

高端网站建设的价格网络推广软文

高端网站建设的价格,网络推广软文,烟台景明网络,网站怎么做网上报名Angular 动态组件 实现步骤 DirectiveHostComponent动态组件AdService配置AppModule需要了解的概念Directive 我们需要一个Directive来标记动态组件是在哪个容器组件内部进行渲染的。这个Directive可以获取对容器组件的引用。仅此而已。import { Directive, ViewContainerRef }…

Angular 动态组件

实现步骤

  • Directive
  • HostComponent
  • 动态组件
  • AdService
  • 配置AppModule
  • 需要了解的概念

Directive

  • 我们需要一个Directive来标记动态组件是在哪个容器组件内部进行渲染的。
  • 这个Directive可以获取对容器组件的引用。
  • 仅此而已。
import { Directive, ViewContainerRef } from '@angular/core';@Directive({selector: '[appAd]',
})
export class AdDirective {constructor(public viewContainerRef: ViewContainerRef) { }
}

HostComponent

  • 我们需要一个容器组件,所有动态组件都是在这个容器组件中创建,销毁,重新创建。。。
  • 需要将动态组件需要展示的数据和动态组件进行绑定。
import { Component, Input, AfterViewInit, ViewChild, ComponentFactoryResolver, OnDestroy } from '@angular/core';import { AdDirective } from './ad.directive';
import { AdItem }      from './ad-item';
import { AdComponent } from './ad.component';@Component({selector: 'app-add-banner',template: `<div class="ad-banner"><h3>Advertisements</h3><!-- hostElement 在此! --><ng-template appAd></ng-template></div>`
})
export class AdBannerComponent implements AfterViewInit, OnDestroy {@Input() ads: AdItem[];currentAddIndex: number = -1;@ViewChild(AdDirective) adHost: AdDirective;subscription: any;interval: any;constructor(private componentFactoryResolver: ComponentFactoryResolver) { }// 在 view 初始化结束后才开始创建动态组件ngAfterViewInit() {this.loadComponent();this.getAds();}ngOnDestroy() {clearInterval(this.interval);}loadComponent() {this.currentAddIndex = (this.currentAddIndex + 1) % this.ads.length;let adItem = this.ads[this.currentAddIndex];// 这里使用了工厂模式。其实Angular对于模板中出现的每一个Component都会创建一个ComponentFactory。在创建销毁时// 实际上使用的是组件工厂来创建组件的新实例。let componentFactory = this.componentFactoryResolver.resolveComponentFactory(adItem.component);let viewContainerRef = this.adHost.viewContainerRef;viewContainerRef.clear();// 传入对应的组件工厂来创建新的组件,并保存新组建的引用。let componentRef = viewContainerRef.createComponent(componentFactory);// 绑定数据(<AdComponent>componentRef.instance).data = adItem.data;}getAds() {this.interval = setInterval(() => {this.loadComponent();}, 3000);}
}
// add-item.ts
import { Type } from '@angular/core';export class AdItem {constructor(public component: Type<any>, public data: any) {}
}
// ad.component.ts
export interface AdComponent {data: any;
}

在AppComponent 中使用

// app.component.tsimport { Component, OnInit } from '@angular/core';import { AdService }         from './ad.service';
import { AdItem }            from './ad-item';@Component({selector: 'app-root',template: `<div><app-add-banner [ads]="ads"></app-add-banner></div>`
})
export class AppComponent implements OnInit {ads: AdItem[];constructor(private adService: AdService) {}ngOnInit() {this.ads = this.adService.getAds();}
}

创建动态组件

// hero-job-ad.component.ts
import { Component, Input } from '@angular/core';import { AdComponent }      from './ad.component';@Component({template: `<div class="job-ad"><h4>{{data.headline}}</h4>{{data.body}}</div>`
})
export class HeroJobAdComponent implements AdComponent {@Input() data: any;}
// hero-profile.component.ts
import { Component, Input }  from '@angular/core';import { AdComponent }       from './ad.component';@Component({template: `<div class="hero-profile"><h3>Featured Hero Profile</h3><h4>{{data.name}}</h4><p>{{data.bio}}</p><strong>Hire this hero today!</strong></div>`
})
export class HeroProfileComponent implements AdComponent {@Input() data: any;
}

创建service

import { Injectable }           from '@angular/core';import { HeroJobAdComponent }   from './hero-job-ad.component';
import { HeroProfileComponent } from './hero-profile.component';
import { AdItem }               from './ad-item';@Injectable()
export class AdService {getAds() {return [new AdItem(HeroProfileComponent, {name: 'Bombasto', bio: 'Brave as they come'}),new AdItem(HeroProfileComponent, {name: 'Dr IQ', bio: 'Smart as they come'}),new AdItem(HeroJobAdComponent,   {headline: 'Hiring for several positions',body: 'Submit your resume today!'}),new AdItem(HeroJobAdComponent,   {headline: 'Openings in all departments',body: 'Apply today'}),];}
}

配置 AppModule

// app.module.ts
import { BrowserModule }        from '@angular/platform-browser';
import { NgModule }             from '@angular/core';
import { AppComponent }         from './app.component';
import { HeroJobAdComponent }   from './hero-job-ad.component';
import { AdBannerComponent }    from './ad-banner.component';
import { HeroProfileComponent } from './hero-profile.component';
import { AdDirective }          from './ad.directive';
import { AdService }            from './ad.service';@NgModule({imports: [ BrowserModule ],providers: [AdService],declarations: [ AppComponent,AdBannerComponent,HeroJobAdComponent,HeroProfileComponent,AdDirective ],// 注意这里,需要手动引入动态组件的class,放入 entryComponents数组中,这样// Angular才能为动态组件创建组件工厂。如果不写,Angular在模板中不会发现这两个组件的具体引用,// 可能在打包时将组件代码排除。entryComponents: [ HeroJobAdComponent, HeroProfileComponent ],bootstrap: [ AppComponent ]
})
export class AppModule {constructor() {}
}
ng serve

完成。

需要了解的概念

  • ViewContainerRef
  • ViewChild
  • ComponentFactoryResolver
  • ComponentFactory
  • ComponentRef

转载于:https://www.cnblogs.com/zhangfengyang/p/8432493.html

http://www.lbrq.cn/news/2768995.html

相关文章:

  • 有没有什么推荐的网站知名网页设计公司
  • 怎么做离线网站营销策划书模板
  • 好一点的网站是怎么做的免费大数据查询平台
  • 做企业网站注意什么优化师助理
  • 企业网站代码模板陕西百度推广的代理商
  • wordpress php学习志鸿优化设计
  • 海南州商城网站建设网页设计排版布局技巧
  • 做网站的术语网络营销方案有哪些
  • 游乐园网站建设长沙互联网推广公司
  • 哪个网站做美食视频软件网站如何注册
  • 博客建站模板全球搜
  • 需求登记网站怎么做百度店铺
  • 烟台网站定制排名国外推广网站
  • 做新闻网站服务器选购软文宣传
  • 建立专业的官方网站手机端搜索引擎排名
  • 深圳免费网站建设怎么做网络营销推广啊
  • dede网站百度广告联盟网站
  • 苏中建设官方网站seo大牛
  • 全球最大的设计网站刷seo关键词排名软件
  • 做简报的网站百度网址查询
  • 沉默是金seo推广沧州公司电话
  • 海南省建设设厅官方网站网站优化外包多少钱
  • 做网站必须在工信部备案吗附近的成人电脑培训班
  • 怎样把域名和做的网站连接不上白杨seo教程
  • 搭建个网站深圳网站seo地址
  • 国际会议网站建设北京疫情最新消息情况
  • 做网站快速排名百度灰色关键词代发
  • 陕西省建设招投标网站前端培训班一般多少钱
  • 现在做跨境电商还能赚钱吗一键关键词优化
  • 温州做外贸网站设计苏州网站维护
  • consul-基础概念
  • AI 药物发现:化学分子到机器学习数值特征的转化——打通“化学空间”与“模型空间”关键路径
  • 数字孪生 :提高制造生产力的智能方法
  • 杂记 05
  • 理解AQS的原理并学习源码
  • Apache IoTDB集群部署实战:1C2D架构的高性能时序数据库搭建与优化指南