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

在Angular中使用NgTemplateOutlet創(chuàng)建可重用組件的流程步驟

 更新時(shí)間:2024年03月01日 08:47:20   作者:白如意i  
在 Angular 中,使用 NgTemplateOutlet 而不是創(chuàng)建特定組件,可以使組件在不修改組件本身的情況下輕松修改為各種用例,在本文中,您將接受一個(gè)現(xiàn)有組件并重寫它以使用 NgTemplateOutlet,需要的朋友可以參考下

簡介

單一職責(zé)原則是指應(yīng)用程序的各個(gè)部分應(yīng)該只有一個(gè)目的。遵循這個(gè)原則可以使您的 Angular 應(yīng)用程序更容易測(cè)試和開發(fā)。

在 Angular 中,使用 NgTemplateOutlet 而不是創(chuàng)建特定組件,可以使組件在不修改組件本身的情況下輕松修改為各種用例。

在本文中,您將接受一個(gè)現(xiàn)有組件并重寫它以使用 NgTemplateOutlet。

先決條件

要完成本教程,您需要:

  • 本地安裝了 Node.js,您可以按照《如何安裝 Node.js 并創(chuàng)建本地開發(fā)環(huán)境》進(jìn)行操作。
  • 一些關(guān)于設(shè)置 Angular 項(xiàng)目的熟悉程度。

本教程已使用 Node v16.6.2、npm v7.20.6 和 @angular/core v12.2.0 進(jìn)行驗(yàn)證。

步驟 1 – 構(gòu)建 CardOrListViewComponent

考慮 CardOrListViewComponent,它根據(jù)其 mode'card''list' 格式中顯示 items。

它由一個(gè) card-or-list-view.component.ts 文件組成:

import {
  Component,
  Input
} from '@angular/core';

@Component({
  selector: 'card-or-list-view',
  templateUrl: './card-or-list-view.component.html'
})
export class CardOrListViewComponent {

  @Input() items: {
    header: string,
    content: string
  }[] = [];

  @Input() mode: string = 'card';

}

以及一個(gè) card-or-list-view.component.html 模板:

<ng-container [ngSwitch]="mode">
  <ng-container *ngSwitchCase="'card'">
    <div *ngFor="let item of items">
      <h1>{{item.header}}</h1>
      <p>{{item.content}}</p>
    </div>
  </ng-container>
  <ul *ngSwitchCase="'list'">
    <li *ngFor="let item of items">
      {{item.header}}: {{item.content}}
    </li>
  </ul>
</ng-container>

這是該組件的使用示例:

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

@Component({
  template: `
    <card-or-list-view
        [items]="items"
        [mode]="mode">
    </card-or-list-view>
`
})
export class UsageExample {
  mode = 'list';
  items = [
    {
      header: 'Creating Reuseable Components with NgTemplateOutlet in Angular',
      content: 'The single responsibility principle...'
    } // ... more items
  ];
}

該組件沒有單一職責(zé),也不夠靈活。它需要跟蹤其 mode 并知道如何在 cardlist 視圖中顯示 items。它只能顯示具有 headercontentitems。

讓我們通過使用模板將組件分解為單獨(dú)的視圖來改變這一點(diǎn)。

步驟 2 – 理解 ng-template 和 NgTemplateOutlet

為了讓 CardOrListViewComponent 能夠顯示任何類型的 items,我們需要告訴它如何顯示它們。我們可以通過給它一個(gè)模板來實(shí)現(xiàn)這一點(diǎn),它可以用來生成 items

模板將使用 <ng-template> 和從 TemplateRefs 創(chuàng)建的 EmbeddedViewRefs。EmbeddedViewRefs 代表具有自己上下文的 Angular 視圖,是最小的基本構(gòu)建塊。

Angular 提供了一種使用這個(gè)從模板生成視圖的概念的方法,即使用 NgTemplateOutlet。

NgTemplateOutlet 是一個(gè)指令,它接受一個(gè) TemplateRef 和上下文,并使用提供的上下文生成一個(gè) EmbeddedViewRef??梢酝ㄟ^ let-{{templateVariableName}}="contextProperty" 屬性在模板上訪問上下文,以創(chuàng)建模板可以使用的變量。如果未提供上下文屬性名稱,它將選擇 $implicit 屬性。

這是一個(gè)示例:

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

@Component({
  template: `
    <ng-container *ngTemplateOutlet="templateRef; context: exampleContext"></ng-container>
    <ng-template #templateRef let-default let-other="aContextProperty">
      <div>
        $implicit = '{{default}}'
        aContextProperty = '{{other}}'
      </div>
    </ng-template>
`
})
export class NgTemplateOutletExample {
  exampleContext = {
    $implicit: 'default context property when none specified',
    aContextProperty: 'a context property'
  };
}

這是示例的輸出:

<div>
  $implicit = 'default context property when none specified'
  aContextProperty = 'a context property'
</div>

defaultother 變量由 let-defaultlet-other="aContextProperty" 屬性提供。

步驟3 – 重構(gòu) CardOrListViewComponent

為了使 CardOrListViewComponent 更加靈活,并允許它顯示任何類型的 items,我們將創(chuàng)建兩個(gè)結(jié)構(gòu)型指令來作為模板。這些模板將分別用于卡片和列表項(xiàng)。

這是 card-item.directive.ts

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

@Directive({
  selector: '[cardItem]'
})
export class CardItemDirective {

  constructor() { }

}

這是 list-item.directive.ts

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

@Directive({
  selector: '[listItem]'
})
export class ListItemDirective {

  constructor() { }

}

CardOrListViewComponent 將導(dǎo)入 CardItemDirectiveListItemDirective

import {
  Component,
  ContentChild,
  Input,
  TemplateRef 
} from '@angular/core';
import { CardItemDirective } from './card-item.directive';
import { ListItemDirective } from './list-item.directive';

@Component({
  selector: 'card-or-list-view',
  templateUrl: './card-or-list-view.component.html'
})
export class CardOrListViewComponent {

  @Input() items: {
    header: string,
    content: string
  }[] = [];

  @Input() mode: string = 'card';

  @ContentChild(CardItemDirective, {read: TemplateRef}) cardItemTemplate: any;
  @ContentChild(ListItemDirective, {read: TemplateRef}) listItemTemplate: any;

}

這段代碼將讀取我們的結(jié)構(gòu)型指令作為 TemplateRefs。

<ng-container [ngSwitch]="mode">
  <ng-container *ngSwitchCase="'card'">
    <ng-container *ngFor="let item of items">
      <ng-container *ngTemplateOutlet="cardItemTemplate"></ng-container>
    </ng-container>
  </ng-container>
  <ul *ngSwitchCase="'list'">
    <li *ngFor="let item of items">
      <ng-container *ngTemplateOutlet="listItemTemplate"></ng-container>
    </li>
  </ul>
</ng-container>

這是該組件的使用示例:

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

@Component({
  template: `
    <card-or-list-view
        [items]="items"
        [mode]="mode">
      <div *cardItem>
        靜態(tài)卡片模板
      </div>
      <li *listItem>
        靜態(tài)列表模板
      </li>
    </card-or-list-view>
`
})
export class UsageExample {
  mode = 'list';
  items = [
    {
      header: '使用 NgTemplateOutlet 在 Angular 中創(chuàng)建可重用組件',
      content: '單一職責(zé)原則...'
    } // ... 更多項(xiàng)
  ];
}

通過這些更改,CardOrListViewComponent 現(xiàn)在可以根據(jù)提供的模板以卡片或列表形式顯示任何類型的項(xiàng)。目前,模板是靜態(tài)的。

我們需要做的最后一件事是通過為它們提供上下文來使模板變得動(dòng)態(tài):

<ng-container [ngSwitch]="mode">
  <ng-container *ngSwitchCase="'card'">
    <ng-container *ngFor="let item of items">
      <ng-container *ngTemplateOutlet="cardItemTemplate; context: {$implicit: item}"></ng-container>
    </ng-container>
  </ng-container>
  <ul *ngSwitchCase="'list'">
    <li *ngFor="let item of items">
      <ng-container *ngTemplateOutlet="listItemTemplate; context: {$implicit: item}"></ng-container>
    </li>
  </ul>
</ng-container>

這是該組件的使用示例:

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

@Component({
  template: `
    <card-or-list-view
        [items]="items"
        [mode]="mode">
      <div *cardItem="let item">
        <h1>{{item.header}}</h1>
        <p>{{item.content}}</p>
      </div>
      <li *listItem="let item">
        {{item.header}}: {{item.content}}
      </li>
    </card-or-list-view>
`
})
export class UsageExample {
  mode = 'list';
  items = [
    {
      header: '使用 NgTemplateOutlet 在 Angular 中創(chuàng)建可重用組件',
      content: '單一職責(zé)原則...'
    } // ... 更多項(xiàng)
  ];
}

有趣的是,我們使用了星號(hào)前綴和微語法來實(shí)現(xiàn)語法糖。這與以下代碼是相同的:

<ng-template cardItem let-item>
  <div>
    <h1>{{item.header}}</h1>
    <p>{{item.content}}</p>
  </div>
</ng-template>

就是這樣!我們擁有了原始功能,但現(xiàn)在可以通過修改模板來顯示任何我們想要的內(nèi)容,而 CardOrListViewComponent 的責(zé)任更少了。我們可以向項(xiàng)上下文中添加更多內(nèi)容,比如類似于 ngForfirstlast,或者顯示完全不同類型的 items

結(jié)論

在本文中,您將一個(gè)現(xiàn)有的組件重寫,以使用 NgTemplateOutlet。

如果您想了解更多關(guān)于 Angular 的內(nèi)容,請(qǐng)查看我們的 Angular 專題頁面,了解相關(guān)練習(xí)和編程項(xiàng)目。

以上就是在Angular中使用NgTemplateOutlet創(chuàng)建可重用組件的流程步驟的詳細(xì)內(nèi)容,更多關(guān)于Angular NgTemplateOutlet可重用組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • angularJs中$http獲取后臺(tái)數(shù)據(jù)的實(shí)例講解

    angularJs中$http獲取后臺(tái)數(shù)據(jù)的實(shí)例講解

    今天小編就為大家分享一篇angularJs中$http獲取后臺(tái)數(shù)據(jù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 詳解AngularJS中自定義過濾器

    詳解AngularJS中自定義過濾器

    過濾器(filter)正如其名,作用就是接收一個(gè)輸入,通過某個(gè)規(guī)則進(jìn)行處理,然后返回處理后的結(jié)果。主要用在數(shù)據(jù)的格式化上,例如獲取一個(gè)數(shù)組中的子集,對(duì)數(shù)組中的元素進(jìn)行排序等
    2015-12-12
  • Angular整合zTree的示例代碼

    Angular整合zTree的示例代碼

    本篇文章主要介紹了Angular整合zTree的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Angular中樣式綁定解決方案

    Angular中樣式綁定解決方案

    這篇文章主要介紹了Angular中樣式綁定解決方案,使用ngClass和ngStyle可以進(jìn)行樣式的綁定,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • 詳解AngularJs中$sce與$sceDelegate上下文轉(zhuǎn)義服務(wù)

    詳解AngularJs中$sce與$sceDelegate上下文轉(zhuǎn)義服務(wù)

    這篇文章給大家詳細(xì)介紹了AngularJs提供的嚴(yán)格上下文轉(zhuǎn)義服務(wù)$sce與$sceDelegate,文中介紹的很詳細(xì),有需要的朋友們可以參考借鑒。
    2016-09-09
  • angularjs自定義過濾器demo示例

    angularjs自定義過濾器demo示例

    這篇文章主要介紹了angularjs自定義過濾器,結(jié)合完整實(shí)例形式分析了angularjs自定義過濾器相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-08-08
  • angular4 JavaScript內(nèi)存溢出問題

    angular4 JavaScript內(nèi)存溢出問題

    本篇文章主要介紹了angular4 JavaScript內(nèi)存溢出問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • 詳解Angular結(jié)構(gòu)型指令模塊和樣式

    詳解Angular結(jié)構(gòu)型指令模塊和樣式

    本文主要介紹了Angular的結(jié)構(gòu)性指令模塊和樣式,對(duì)此感興趣的同學(xué),可以參考下。
    2021-05-05
  • AngularJS基礎(chǔ) ng-include 指令簡單示例

    AngularJS基礎(chǔ) ng-include 指令簡單示例

    本文主要介紹AngularJS ng-include 指令,這里對(duì)ng-include的基本知識(shí)做了整理,并附有代碼實(shí)例,有需要的朋友可以參考下
    2016-08-08
  • 詳解angular ui-grid之過濾器設(shè)置

    詳解angular ui-grid之過濾器設(shè)置

    本篇文章主要介紹了詳解angular ui-grid之過濾器設(shè)置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評(píng)論

顺义区| 韶山市| 广东省| 广南县| 牙克石市| 谢通门县| 科技| 屯留县| 泾川县| 安阳县| 南澳县| 小金县| 麻江县| 寿光市| 崇州市| 德令哈市| 旺苍县| 玉林市| 远安县| 太和县| 玉龙| 西贡区| 苍梧县| 道真| 周口市| 华宁县| 柳河县| 阜平县| 池州市| 六枝特区| 哈尔滨市| 巩义市| 华宁县| 乐亭县| 峡江县| 胶南市| 深泽县| 宁波市| 老河口市| 栾川县| 铜梁县|