溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Angular組件實(shí)現(xiàn)內(nèi)容投影

發(fā)布時間:2021-08-09 11:27:51 來源:億速云 閱讀:215 作者:Leah 欄目:web開發(fā)

這篇文章給大家介紹如何使用Angular組件實(shí)現(xiàn)內(nèi)容投影,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

1. 投影一塊內(nèi)容

容器組件這樣寫

<div>
  編號1
  <ng-content></ng-content>
</div>

業(yè)務(wù)組件這樣用

<app-page-container>
	未指定投影位置的內(nèi)容會被投影到無select屬性的區(qū)域
</app-page-container>
2. 投影多塊內(nèi)容/組件

容器組件這樣寫

  • 使用標(biāo)簽鎖定投影位置

  • 使用class鎖定投影位置

  • 用自定義組件名稱鎖定投影位置

  • 使用自定義屬性鎖定投影位置

<div>
  編號2
  <ng-content select="h4"></ng-content>
  <ng-content select=".my-class"></ng-content>
  <ng-content select="app-my-hello"></ng-content>
  <ng-content select="[content]"></ng-content>
</div>

業(yè)務(wù)組件這樣用

<app-page-container>
  <h4>使用標(biāo)簽鎖定投影位置</h4>
  <div class="my-class">使用class鎖定投影位置</div>
  <app-my-hello>使用自定義組件名稱鎖定投影位置</app-my-hello>
  <div content>使用自定義屬性鎖定投影位置</div>
</app-page-container>

演示

如何使用Angular組件實(shí)現(xiàn)內(nèi)容投影

3. 投影子元素

使用ng-container來包裹子元素,減少不必要的dom層,類似vue中的template

容器組件這樣寫

<div>
  編號4
  <ng-content select="question"></ng-content>
</div>

業(yè)務(wù)組件這樣寫

<app-page-container>
  <ng-container ngProjectAs="question">
    <p>內(nèi)容投影酷嗎?</p>
    <p>內(nèi)容投影酷嗎?</p>
    <p>內(nèi)容投影酷嗎?</p>
    <p>內(nèi)容投影酷嗎?</p>
  </ng-container>
</app-page-container>
4. 有條件的內(nèi)容投影

中文網(wǎng)的描述:

  • 如果你的組件需要_有條件地_渲染內(nèi)容或多次渲染內(nèi)容,則應(yīng)配置該組件以接受一個 ng-template 元素,其中包含要有條件渲染的內(nèi)容。

  • 在這種情況下,不建議使用 ng-content 元素,因?yàn)橹灰M件的使用者提供了內(nèi)容,即使該組件從未定義 ng-content 元素或該 ng-content 元素位于 ngIf 語句的內(nèi)部,該內(nèi)容也總會被初始化。

  • 使用 ng-template 元素,你可以讓組件根據(jù)你想要的任何條件顯式渲染內(nèi)容,并可以進(jìn)行多次渲染。在顯式渲染 ng-template 元素之前,Angular 不會初始化該元素的內(nèi)容。

使用ng-container定義我們的投影區(qū)塊

  • 使用ngTemplateOutlet指令來渲染ng-template元素。

  • 通過內(nèi)置的動態(tài)指令*ngIf來控制是否渲染投影。

<div>
  編號3
  <ng-content select="[button]"></ng-content>
  <p *ngIf="expanded">
    <ng-container [ngTemplateOutlet]="content.templateRef"> </ng-container>
  </p>
</div>

在業(yè)務(wù)組件中我們使用ng-template來包裹我們的實(shí)際元素。

my-hello組件只在ngOnInit()做日志輸出來觀察打印情況。

<app-page-container>
  <div button>
    <button appToggle>切換</button>
  </div>
  <ng-template appContent>
    <app-my-hello>有條件的內(nèi)容投影~</app-my-hello>
  </ng-template>
</app-page-container>

現(xiàn)在你會發(fā)現(xiàn)頁面并沒有像前面那么順利的正常渲染,因?yàn)槲覀兊倪壿嬤€沒有串通,我們繼續(xù)。創(chuàng)建一個指令,并在NgModule中注冊,一定要注冊才能用哦~

指令需要注冊哦~

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

@Directive({
  selector: '[appContent]',
})
export class ContentDirective {
  constructor(public templateRef: TemplateRef<unknown>) {}
}

我們再定義一個指令來控制組件中顯示/隱藏的標(biāo)識

指令需要注冊哦~

@Directive({
  selector: '[appToggle]',
})
export class ToggleDirective {
  @HostListener('click') toggle() {
    this.app.expanded = !this.app.expanded;
  }
  constructor(public app: PageContainerComponent) {}
}

在我們的容器組件中申明剛才定義的內(nèi)容指令,頁面目前不報(bào)錯咯~

export class PageContainerComponent implements OnInit {

  expanded: boolean = false;

  @ContentChild(ContentDirective)
  content!: ContentDirective;

}

通過日志可以看到我們在切換容器組件的expanded標(biāo)識時,只有開啟狀態(tài)my-hello組件才會初始化,下面的這個ngIf雖然在頁面看不到渲染的內(nèi)容,但組件實(shí)實(shí)在在被初始化過了。

<div *ngIf="false">
  <ng-content *ngIf="false" select="app-my-hello"></ng-content>
</div>
5. @ContentChild & @ContentChildren

使用這兩個裝飾器來對被投影的組件進(jìn)行操作

使用注解在業(yè)務(wù)組件中定義被投影的組件

@ContentChild(HelloWorldComp)
helloComp: HelloWorldComp;

@ContentChildren(HelloWorldComp)
helloComps: QueryList<HelloWorldComp>;

ngAfterContentInit()鉤子執(zhí)行后對被投影組件進(jìn)行操作

6. @ViewChild & @ViewChildren

使用這兩個裝飾器來對指接子組件進(jìn)行操作

使用注解在業(yè)務(wù)組件中定義子組件

@ViewChild(HelloWorldComp)
helloComp: HelloWorldComp;
  
@ViewChildren(HelloWorldComp)
helloComps QueryList<HelloWorldComp>;

ngAfterViewInit()鉤子執(zhí)行后對直接子組件進(jìn)行操作

關(guān)于如何使用Angular組件實(shí)現(xiàn)內(nèi)容投影就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI