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

動(dòng)手寫(xiě)一個(gè)angular版本的Message組件的方法

 更新時(shí)間:2017年12月16日 09:36:58   作者:木易木啊  
本篇文章主要介紹了動(dòng)手寫(xiě)一個(gè)angular版本的Message組件的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

學(xué)習(xí)一個(gè)框架或庫(kù)的最好方法是看官方文檔,并著手去寫(xiě)例子。最近在利用空閑的時(shí)間學(xué)習(xí)angular,那今天就嘗試寫(xiě)一個(gè)message組件,并通過(guò)message服務(wù)動(dòng)態(tài)加載message組件。
我所參與的項(xiàng)目基本是用jquery完成的。之前,在項(xiàng)目中自己動(dòng)手寫(xiě)過(guò)一個(gè)簡(jiǎn)單的message插件,樣子如下圖。

那現(xiàn)在就使用angular(版本5.0.0)來(lái)實(shí)現(xiàn)message組件。

message組件

message組件要根據(jù)傳入的類型、消息和duration來(lái)顯示。創(chuàng)建三個(gè)文件:message.component.ts,message.component.html,message.component.css,代碼如下。

//message.component.ts
import {Component,Input,OnInit,ChangeDetectionStrategy} from '@angular/core';
import {
  trigger,
  state,
  style,
  transition,
  animate
 } from '@angular/animations';
const mapping={
  success:'glyphicon-ok-sign',
  warning:'glyphicon-exclamation-sign',
  error:'glyphicon-exclamation-sign',
  info:'glyphicon-ok-circle'
}
@Component({
  selector:'upc-ng-message',
  templateUrl:'./message.component.html',
  styleUrls:['./message.component.css'],
  changeDetection:ChangeDetectionStrategy.OnPush
})
export class MessageComponent implements OnInit{
  ngOnInit(): void {
    this.typeClass=['upc-message-' + this.msgType];
    this.typeIconClass=[mapping[this.msgType]];
  }
  @Input() msgType:'success' | 'info' | 'warning' | 'error'='info'

  @Input() payload:string = ''

  private typeClass
  private typeIconClass
}

<!--*message.component.html-->
<div class="upc-message">
    <div class="upc-message-content" [ngClass]="typeClass">
      <i class="glyphicon" [ngClass]="typeIconClass"></i>
      {{payload}}
    </div>
</div>
.upc-message {
  position: fixed;
  z-index: 1999;
  width: 100%;
  top: 36px;
  left: 0;
  pointer-events: none;
  padding: 8px;
  text-align: center;
 }
 .upc-message i {
   margin-right: 8px;
   font-size: 14px;
   top: 1px;
   position: relative;
 }
 .upc-message-success i {
   color: green;
 }
 .upc-message-warning i {
   color: yellow;
 }
 .upc-message-error i {
   color: red;
 }
 .upc-message-content {
   padding: 8px 16px;
   -ms-border-radius: 4px;
   border-radius: 4px;
   -webkit-box-shadow: 0 2px 8px #000000;
   -ms-box-shadow: 0 2px 8px #000000;
   box-shadow: 0 2px 8px #000000;
   box-shadow: 0 2px 8px rgba(0,0,0,.2);
   background: #fff;
   display: inline-block;
   pointer-events: all;
 }

ComponentLoader

通過(guò)官方文檔動(dòng)態(tài)組件一節(jié),可以了解動(dòng)態(tài)創(chuàng)建組件需要通過(guò)ComponentFactoryResolver來(lái)完成。使用ComponentFactoryResolver創(chuàng)建ComponentFactory,再通過(guò)ComponentFactory的create方法創(chuàng)建組件??垂俜轿臋n中API的說(shuō)明,ComponentFactory的create方法至少需要一個(gè)injector參數(shù),而injector的創(chuàng)建在文檔中也有提到,其中參數(shù)providers為需要注入的類。再梳理下整個(gè)過(guò)程:

  1. 提供providers
  2. 創(chuàng)建Injector實(shí)例
  3. 創(chuàng)建ComponetFactory
  4. 使用ComponetFactory創(chuàng)建ComponentRef
//ComponentFactory的create方法
create(injector: Injector, projectableNodes?: any[][], rootSelectorOrNode?: string|any, ngModule?: NgModuleRef<any>): ComponentRef<C>

//使用Injector的create創(chuàng)建injector實(shí)例
static create(providers: StaticProvider[], parent?: Injector): Injector

為了代碼的復(fù)用,這里創(chuàng)建通用的loader類來(lái)完成組件的動(dòng)態(tài)創(chuàng)建。其中,attch方法用于初始化ComponetFactory(參數(shù)為組件類型);to方法用于標(biāo)識(shí)組件的父容器;provider方法用于初始化可注入的類;create方法用于創(chuàng)建組件并手動(dòng)變更檢測(cè);remove方法用于移除組件。

import {
  ComponentFactoryResolver,
  ComponentFactory,
  ComponentRef,
  Type,
  Injector,
  Provider,
  ElementRef
} from '@angular/core';
export class ComponentLoader<T>{
  constructor(private _cfr: ComponentFactoryResolver,
    private _injector: Injector) {
  }
  private _componentFactory: ComponentFactory<T>
  attch(componentType: Type<T>): ComponentLoader<T> {
    this._componentFactory = this._cfr.resolveComponentFactory<T>(componentType);
    return this;
  }
  private _parent: Element
  to(parent: string | ElementRef): ComponentLoader<T> {
    if (parent instanceof ElementRef) {
      this._parent = parent.nativeElement;
    } else {
      this._parent = document.querySelector(parent);
    }

    return this;
  }
  private _providers: Provider[] = [];
  provider(provider: Provider) {
    this._providers.push(provider);
  }
  create(opts: {}): ComponentRef<T> {
    const injector = Injector.create(this._providers as any[], this._injector);
    const componentRef = this._componentFactory.create(injector);
    Object.assign(componentRef.instance, opts);
    if (this._parent) {
      this._parent.appendChild(componentRef.location.nativeElement);
    }
    componentRef.changeDetectorRef.markForCheck();
    componentRef.changeDetectorRef.detectChanges();
    return componentRef;
  }
  remove(ref:ComponentRef<T>){
    if(this._parent){
      this._parent.removeChild(ref.location.nativeElement)
    }
    ref=null;
  }
}

同時(shí),為了便于loader的創(chuàng)建,再創(chuàng)建LoaderFactory類,代碼如下:

import {
  ComponentFactoryResolver,
  Injector,
  Injectable,
  ElementRef
} from '@angular/core';
import { ComponentLoader } from './component-loader.class';

@Injectable()
export class ComponentLoaderFactory {
  constructor(private _injector: Injector,
    private _cfr: ComponentFactoryResolver) {

  }

  create<T>(): ComponentLoader<T> {
    return new ComponentLoader(this._cfr, this._injector);
  }
}

message service

message service提供顯示message的API,代碼如下:

import {Injectable,Injector} from '@angular/core';
import { ComponentLoaderFactory } from '../component-loader/component-loader.factory';
import {MessageComponent} from './message.component';
import {ComponentLoader} from '../component-loader/component-loader.class';

@Injectable()
export class MessageService{
  constructor(private _clf:ComponentLoaderFactory,private _injector:Injector){
    this.loader=this._clf.create<MessageComponent>();
  }
  private loader:ComponentLoader<MessageComponent>
  private createMessage(t,c,duration=2000){
    this.loader.attch(MessageComponent).to('body');
    const opts = {
      msgType: t,
      payload:c
    };
    const ref = this.loader.create(opts);
    ref.changeDetectorRef.markForCheck();
    ref.changeDetectorRef.detectChanges();
    let self=this;
    let st = setTimeout(() => {
      self.loader.remove(ref);
    }, duration);
  }
  public info(payload,duration?) {
    this.createMessage('info',payload,duration);
  }
  public success(payload,duration?) {
    this.createMessage('success',payload,duration);
  }
  public error(payload,duration?) {
    this.createMessage('error',payload,duration);
  }
  public warning(payload,duration?) {
    this.createMessage('warning',payload,duration);
  }
}

message.module

最后,增加message.module.ts。記得要把動(dòng)態(tài)創(chuàng)建的組件添加到entryComponents數(shù)組中。

import {NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import {MessageComponent} from './message.component';
import {MessageService} from './message.service';
import {ComponentLoaderFactory} from '../component-loader/component-loader.factory';

@NgModule({
  imports:[CommonModule],
  declarations:[MessageComponent],
  providers:[MessageService,ComponentLoaderFactory],
  entryComponents:[MessageComponent],
  exports:[MessageComponent]
})
export class MessageModule{
}

使用方法

注入MessageService,調(diào)用API使用Message組件。

this._msgService.success('成功了!');

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Angular 自定義結(jié)構(gòu)指令

    詳解Angular 自定義結(jié)構(gòu)指令

    本篇文章主要介紹了詳解Angular 自定義結(jié)構(gòu)指令,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 詳解Angular數(shù)據(jù)綁定及其實(shí)現(xiàn)方式

    詳解Angular數(shù)據(jù)綁定及其實(shí)現(xiàn)方式

    數(shù)據(jù)綁定是將應(yīng)用程序UI或用戶界面綁定到模型的機(jī)制。使用數(shù)據(jù)綁定,用戶將能夠使用瀏覽器來(lái)操縱網(wǎng)站上存在的元素。
    2021-05-05
  • Angular.js中angular-ui-router的簡(jiǎn)單實(shí)踐

    Angular.js中angular-ui-router的簡(jiǎn)單實(shí)踐

    本篇文章主要介紹了Angular.js中angular-ui-router的簡(jiǎn)單實(shí)踐,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • angular共享依賴的解決方案分享

    angular共享依賴的解決方案分享

    這篇文章主要給大家介紹了關(guān)于angular共享依賴解決方案的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • angular同一頁(yè)面跳轉(zhuǎn)重新執(zhí)行的實(shí)現(xiàn)方法

    angular同一頁(yè)面跳轉(zhuǎn)重新執(zhí)行的實(shí)現(xiàn)方法

    這篇文章主要介紹了angular同一頁(yè)面跳轉(zhuǎn)重新執(zhí)行的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 基于angular中的重要指令詳解($eval,$parse和$compile)

    基于angular中的重要指令詳解($eval,$parse和$compile)

    下面小編就為大家?guī)?lái)一篇基于angular中的重要指令詳解($eval,$parse和$compile)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • AngularJS基礎(chǔ) ng-csp 指令詳解

    AngularJS基礎(chǔ) ng-csp 指令詳解

    本文主要介紹AngularJS ng-csp 指令,這里對(duì)ng-csp 的基礎(chǔ)資料的整理,并附代碼實(shí)例和實(shí)現(xiàn)效果圖,有需要的小伙伴參考下
    2016-08-08
  • 詳解Angular2 之 結(jié)構(gòu)型指令

    詳解Angular2 之 結(jié)構(gòu)型指令

    本篇文章主要介紹了詳解Angular2 之 結(jié)構(gòu)型指令,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 詳解如何在Angular應(yīng)用中發(fā)起HTTP?302 redirect

    詳解如何在Angular應(yīng)用中發(fā)起HTTP?302 redirect

    這篇文章主要介紹了如何在Angular應(yīng)用中發(fā)起HTTP 302 redirect詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • AngularJS中指令的四種基本形式實(shí)例分析

    AngularJS中指令的四種基本形式實(shí)例分析

    這篇文章主要介紹了AngularJS中指令的四種基本形式,結(jié)合實(shí)例形式分析了AngularJS指令的定義、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-11-11

最新評(píng)論

遵义县| 隆德县| 儋州市| 上思县| 陇川县| 广灵县| 南和县| 广昌县| 泾川县| 达州市| 赤城县| 嘉定区| 方城县| 龙泉市| 巴青县| 东乡县| 乐业县| 道真| 古蔺县| 米林县| 察隅县| 基隆市| 华坪县| 尚志市| 原平市| 大化| 郴州市| 石泉县| 黔东| 汤原县| 鸡西市| 昌江| 中方县| 雅安市| 吐鲁番市| 德庆县| 拉孜县| 丹棱县| 凤冈县| 肇庆市| 家居|