最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular通過指令動(dòng)態(tài)添加組件問題

 更新時(shí)間:2018年07月09日 11:52:10   作者:Shapeying  
這篇文章主要介紹了Angular通過指令動(dòng)態(tài)添加組件問題,文中通過寫一個(gè)小組件來簡(jiǎn)單總結(jié)下,需要的朋友可以參考下

之前自己寫的公共組件,都是會(huì)先引入,需要調(diào)起的時(shí)候再通過service控制公共組件狀態(tài)、值、回調(diào)函數(shù)什么的。但是有一些場(chǎng)景不適合這種方式,還是動(dòng)態(tài)添加組件更加好。通過寫過的一個(gè)小組件來總結(jié)下。

創(chuàng)建組件

  場(chǎng)景:鼠標(biāo)移動(dòng)到圖標(biāo)上時(shí),展示解釋性的說明文字。那就需要?jiǎng)?chuàng)建一個(gè)普通的tooltip組件。如下:

<aside class="hover-tip-wrapper">
 <span>{{tipText}}</span>
</aside>
import { Component, OnInit } from '@angular/core';
@Component({
 selector: 'app-hovertip',
 templateUrl: './hovertip.component.html',
 styleUrls: ['./hovertip.component.scss']
})
export class HovertipComponent implements OnInit {
 public tipText: string;
 constructor() { }
 ngOnInit() {
 }
}
.hover-tip-wrapper{
 width: max-content;
 position: absolute;
 height: 30px;
 line-height: 30px;
 bottom: calc(100% + 5px);
 right: calc( -10px - 100%);
 background-color: rgba(#000000,.8);
 padding: 0 5px;
 border-radius: 3px;
 &::after{
 content: '';
 position: absolute;
 height: 0;
 width: 0;
 border: 4px solid transparent;
 border-top-color: rgba(#000000,.8);
 left: 10px;
 top: 100%;
 }
 span {
 color: #ccc;
 font-size: 12px;
 }
}

  非常簡(jiǎn)單的一個(gè)組件,tipText來接收需要展示的文字。

  需要注意的是,聲明組件的時(shí)候,除了需要添加到declarations中外,還記得要添加到entryComponents中。

entryComponents: [HovertipComponent],
declarations: [HovertipComponent, HovertipDirective]

  那entryComponents這個(gè)配置項(xiàng)是做什么的呢?看源碼注釋,大概意思就是:Angular會(huì)為此配置項(xiàng)中的組件創(chuàng)建一個(gè)ComponentFactory,并存放在ComponentFactoryResolver中。動(dòng)態(tài)添加組件時(shí),需要用到組件工廠,所以此配置是必不可少的。

創(chuàng)建指令

  通過指令為目標(biāo)元素綁定事件,控制創(chuàng)建組件、傳遞tipText以及組件的銷毀。

import { Input , Directive , ViewContainerRef , ComponentRef, ComponentFactory, HostListener , ComponentFactoryResolver} from '@angular/core';
import { HovertipComponent } from './hovertip.component';
@Directive({
 selector: '[appHovertip]'
})
export class HovertipDirective {
 public hovertip: ComponentRef<HovertipComponent>;
 public factory: ComponentFactory<HovertipComponent>;
 constructor(
 private viewContainer: ViewContainerRef,
 private resolver: ComponentFactoryResolver
 ) {
 // 獲取對(duì)應(yīng)的組件工廠
 this.factory = this.resolver.resolveComponentFactory(HovertipComponent);
 }
 @Input('appHovertip') tipText: string;
 
 // 綁定鼠標(biāo)移入的事件
 @HostListener('mouseenter') onmouseenter() {
   // 清空所有的view 
   this.viewContainer.clear();
 // 創(chuàng)建組件
 this.hovertip = this.viewContainer.createComponent(this.factory);
 // 向組件實(shí)例傳遞參數(shù)
 this.hovertip.instance.tipText = this.tipText;
 }
 
 // 綁定鼠標(biāo)移出時(shí)的事件
 @HostListener('mouseleave') onmouseleave() {
 if (this.hovertip) {
  // 組件銷毀
 this.hovertip.destroy();
 }
 }
}

  通過ViewContainerRef類來管理視圖,這里用到了創(chuàng)建組件。這個(gè) 專欄 解釋的挺清楚的。這里用到了以下兩個(gè)API,清除和創(chuàng)建。

  createComponent方法接受ComponentFactoty類,創(chuàng)建后返回的ComponentRef類,可以獲取到組件實(shí)例(instance),控制組件銷毀。

  大致思路是這樣的,先獲取到了HovertipComponent組件對(duì)于的componentFactory,監(jiān)聽鼠標(biāo)移入事件,在觸發(fā)事件時(shí),通過ViewContainerRef類來創(chuàng)建組件,存下返回的組件componentRef(獲取實(shí)例,銷毀組件時(shí)需要用到),向組件實(shí)例傳遞tipText。監(jiān)聽鼠標(biāo)移出事件,在事件觸發(fā)時(shí),銷毀組件。

使用

  在目標(biāo)元素是綁定指令,同時(shí)傳遞tipText即可。

  可以正常的創(chuàng)建和銷毀。

總結(jié)

  開始做的時(shí)候,主要是對(duì)這幾個(gè)類比較懵,ViewContainerRef、ComponentRef、ComponentFactory、ComponentFactoryResolver等,看看源碼,查查資料,總會(huì)梳理清楚的。

參考資料:

http://www.fzitv.net/article/114683.htm

http://www.fzitv.net/article/112123.htm

相關(guān)文章

最新評(píng)論

班戈县| 多伦县| 枣强县| 精河县| 淅川县| 勐海县| 永兴县| 晋江市| 龙游县| 杭锦后旗| 固镇县| 古田县| 东台市| 布尔津县| 天峻县| 台中市| 理塘县| 德安县| 尖扎县| 大英县| 颍上县| 新竹市| 肥西县| 札达县| 厦门市| 双桥区| 伊通| 安庆市| 施秉县| 壶关县| 策勒县| 昭平县| 凌海市| 上蔡县| 昌都县| 正镶白旗| 桂林市| 正安县| 石楼县| 淮阳县| 金堂县|