不要被术语嵌入包含所迷惑,下面用一个简单的例子将它说明清楚。html
现有一个卡片组件(card component),它由header、body和footer三个部分组成。json
好比,咱们能够这样使用该组件:bootstrap
或者这样:app
亦或者这样布局
总之很是方便。this
问题来了,咱们该如何实现**头部和尾部格式固定,而body中的内容能够动态显示呢?
--答案是使用嵌入包含**。spa
transclusion是一个方法,容许你定义个固定视图模板的同时,还能够经过
<ng-content>
定义一个插槽,以显示动态的内容。
颇有意思吧,下面咱们就来实现一下。code
|- 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
// 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>
。
<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
属性的元素才能够代替插槽。
<ng-content>
中select
属性很是强大。举几个例子,
<!-- card.component.html --> ... <ng-content select="[card-type=body]"></ng-content> ...
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"
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 { }
<!-- 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>
咱们应该使用哪一个选择器呢?属性选择器?html标签?类选择器?
视状况而定。我更偏向于使用属性选择器,由于它更易读。HTML标签易读但须要在元数据中添加schema
属性。
应该尽量避免使用类选择器,由于不直观。你第一眼看到它时,第一反应不是“嵌入包含
”,除非你阅读了组件的源码。固然了,这都取决于你!
好了,就到这里!祝coding愉快!