【Angular 2+】使用实现嵌入包含(transclusion)

1 为何要用transclusion(嵌入包含)

不要被术语嵌入包含所迷惑,下面用一个简单的例子将它说明清楚。html

现有一个卡片组件(card component),它由headerbodyfooter三个部分组成。json

  • 该卡片的布局(3个部分)和颜色(header和footer的背景色为灰色)是固定的
  • header和footer只容许文本内容;
  • body中能够是任何形式的内容。

clipboard.png

好比,咱们能够这样使用该组件:bootstrap

clipboard.png

或者这样:app

clipboard.png

亦或者这样布局

clipboard.png

总之很是方便。this

问题来了,咱们该如何实现**头部和尾部格式固定,而body中的内容能够动态显示呢?
--答案是使用嵌入包含**。spa

2 什么是transclusion(嵌入包含)

transclusion是一个方法,容许你定义个固定视图模板的同时,还能够经过 <ng-content>定义一个插槽,以显示动态的内容。

颇有意思吧,下面咱们就来实现一下。code

3 实现嵌入包含transclusion

3.1 App结构

|- app/
    |- app.component.html
    |- app.component.ts
    |- app.module.ts
    |- card.component.ts
    |- card.component.html
    |- main.ts
|- index.html
|- systemjs.config.js
|- tsconfig.json

3.2 单插槽的嵌入包含

定义组件

// card.component.ts

import { Component, Input, Output } from '@angular/core';
@Component({
  selector: 'card',
  templateUrl: 'card.component.html',
})
export class CardComponent {
    @Input() header: string = 'this is header';   
    @Input() footer: string = 'this is footer';
}

组件模板template:component

<!-- card.component.html -->

<div class="card">
    <div class="card-header">
        {{ header }}
    </div>

    <!-- single slot transclusion here -->
    <ng-content></ng-content>

    <div class="card-footer">
        {{ footer }}
    </div>
</div>

使用组件

如今咱们已经定义好了一个组件,接下来咱们将使用它。orm

<!-- app.component.html -->

<h1>Single slot transclusion</h1>
<card header="my header" footer="my footer">
    <!-- put your dynamic content here -->
    <div class="card-block">
        <h4 class="card-title">You can put any content here</h4>
        <p class="card-text">For example this line of text and</p>
        <a href="#" class="btn btn-primary">This button</a>
      </div>
      <!-- end dynamic content -->
<card>

最后在根模块的declarations中声明添加便可。

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }   from './app.component';
import { CardComponent } from './card.component'; // import card component

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent, CardComponent ], // add in declaration
  bootstrap:    [ AppComponent ],
})

export class AppModule { }

好了,大功告成,保存并运行吧。div.card-block中的内容将会代替<ng-content></ng-content>

3.3 插槽的选择器

<ng-content>接受一个 select属性,让插槽具备选择性。
<!-- card.component.html -->

<div class="card">
    <div class="card-header">
        {{ header }}
    </div>

    <!-- add the select attribute to ng-content -->
    <ng-content select="[card-body]"></ng-content>

    <div class="card-footer">
        {{ footer }}
    </div>
</div>

注意,咱们添加了select=[card-body],这里意思是“让包含card-body属性的元素取代我”。
接着,在html中添加card-body属性。

<!-- app.component.html -->

<h1>Single slot transclusion</h1>
<card header="my header" footer="my footer">

    <div class="card-block" card-body><!--  We add the card-body attribute here -->
        <h4 class="card-title">You can put any content here</h4>
        <p class="card-text">For example this line of text and</p>
        <a href="#" class="btn btn-primary">This button</a>
      </div>

<card>

保存并运行,一切照常运行。
如今,若是移除card-body,卡片body什么也显示不出来,那是由于咱们定义的<ng-content>具备选择性--只有带有card-body属性的元素才能够代替插槽。

3.4 强大的选择器

<ng-content>select属性很是强大。举几个例子,

3.4.1 带值的属性

<!-- card.component.html -->
...
<ng-content select="[card-type=body]"></ng-content>
...

3.4.2 使用CSS选择器

card.component.html

...
<ng-content select=".card-body"></ng-content>
...

app.component.html

...
<div class="card-block card-body">...</div>
...

除此以外,例如select=[card][body],selector=".card.body"

3.4.3 使用HTML标签

card.component.html

...
<ng-content select="card-body"></ng-content>
...

app.component.html

...
<card-body class="card-block">...<card-body>
...

可是,你会遇到一个错误:Unhandled Promise rejection: Template parse errors: 'card-body' is not a known element
Angular 2不认识card-body标签,它既不是指令,也不是组件。一个快速回避该错误的方法是:在模块元数据中添加属性schemas,以下:

// app.module.ts

import { NgModule, NO_ERRORS_SCHEMA }      from '@angular/core'; //
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }   from './app.component';
import { CardComponent } from './card.component';

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent, CardComponent ],
  bootstrap:    [ AppComponent ],
  schemas:      [ NO_ERRORS_SCHEMA ] // add this line
})

export class AppModule { }

3.5 多插槽的嵌入包含

<!-- card.component.html -->

<div class="card">
    <div class="card-header">
    <!-- header slot here -->
        <ng-content select="card-header"></ng-content>
    </div>
    <!-- body slot here -->
    <ng-content select="card-body"></ng-content>
    <div class="card-footer">
    <!-- footer -->
        <ng-content select="card-footer"></ng-content>
    </div>
</div>

app

<!-- app.component.html -->

<h1>Multi slot transclusion</h1>
<card>
    <!-- header -->
    <card-header>
        New <strong>header</strong>
    </card-header>

    <!-- body -->
    <card-body>
        <div class="card-block">
            <h4 class="card-title">You can put any content here</h4>
            <p class="card-text">For example this line of text and</p>
            <a href="#" class="btn btn-primary">This button</a>
          </div>
    </card-body>

    <!-- footer -->
    <card-footer>
        New <strong>footer</strong>
    </card-footer>
<card>

4 总结

咱们应该使用哪一个选择器呢?属性选择器?html标签?类选择器?

视状况而定。我更偏向于使用属性选择器,由于它更易读。HTML标签易读但须要在元数据中添加schema属性。

应该尽量避免使用类选择器,由于不直观。你第一眼看到它时,第一反应不是“嵌入包含”,除非你阅读了组件的源码。固然了,这都取决于你!

好了,就到这里!祝coding愉快!

相关文章
相关标签/搜索