angular組件繼承的實(shí)現(xiàn)方法第1/2頁
Angular 2.3 版本中引入了組件繼承的功能,該功能非常強(qiáng)大,能夠大大增加我們組件的可復(fù)用性。
組件繼承涉及以下的內(nèi)容:
Metadata:如 @Input()、@Output()、@ContentChild/Children、@ViewChild/Children 等。在派生類中定義的元數(shù)據(jù)將覆蓋繼承鏈中的任何先前的元數(shù)據(jù),否則將使用基類元數(shù)據(jù)。
Constructor:如果派生類未聲明構(gòu)造函數(shù),它將使用基類的構(gòu)造函數(shù)。這意味著在基類構(gòu)造函數(shù)注入的所有服務(wù),子組件都能訪問到。
Lifecycle hooks:如果基類中包含生命周期鉤子,如 ngOnInit、ngOnChanges 等。盡管在派生類沒有定義相應(yīng)的生命周期鉤子,基類的生命周期鉤子會(huì)被自動(dòng)調(diào)用。
需要注意的是,模板是不能被繼承的 ,因此共享的 DOM 結(jié)構(gòu)或行為需要單獨(dú)處理。了解詳細(xì)信息,請(qǐng)查看 - properly support inheritance。
接下來我們來快速體驗(yàn)的組件繼承的功能并驗(yàn)證以上的結(jié)論,具體示例如下(本文所有示例基于的 Angular 版本是 - 4.0.1):
exe-base.component.ts
import { Component, ElementRef, Input, HostBinding, HostListener, OnInit } from '@angular/core'; @Component({
selector: 'exe-base', // template will not be inherited template: `
<div>
exe-base:我是base組件么? - {{isBase}}
</div>
` }) export class BaseComponent implements OnInit { @Input() isBase: boolean = true; @HostBinding('style.color') color = 'blue'; // will be inherited @HostListener('click', ['$event']) // will be inherited onClick(event: Event) { console.log(`I am BaseComponent`);
} constructor(protected eleRef: ElementRef) { }
ngOnInit() { console.dir('BaseComponent:ngOnInit method has been called');
}
}
exe-inherited.component.ts
import { Component, HostListener, OnChanges, SimpleChanges } from '@angular/core'; import { BaseComponent } from './exe-base.component'; @Component({
selector: 'exe-inherited',
template: `
<div>
exe-inherited:我是base組件么? - {{isBase}}
</div>
` }) export class InheritedComponent extends BaseComponent implements OnChanges { @HostListener('click', ['$event']) // overridden onClick(event: Event) { console.log(`I am InheritedComponent`);
}
ngOnChanges(changes: SimpleChanges) { console.dir(this.eleRef); // this.eleRef.nativeElement:exe-inherited }
}
app.component.ts
import { Component, OnInit } from '@angular/core'; import {ManagerService} from "./manager.service"; @Component({
selector: 'exe-app',
template: `
<exe-base></exe-base>
<hr/>
<exe-inherited [isBase]="false"></exe-inherited>
` }) export class AppComponent {
currentPage: number = 1;
totalPage: number = 5;
}
接下來我們簡要討論一個(gè)可能令人困惑的主題,@Component() 中元數(shù)據(jù)是否允許繼承?答案是否定的,子組件是不能繼承父組件裝飾器中元數(shù)據(jù)。限制元數(shù)據(jù)繼承,從根本上說,是有道理的,因?yàn)槲覀兊脑獢?shù)據(jù)用是來描述組件類的,不同的組件我們是需要不同的元數(shù)據(jù),如 selector、template 等。Angular 2 組件繼承主要還是邏輯層的復(fù)用,具體可以先閱讀完下面實(shí)戰(zhàn)的部分,再好好體會(huì)一下哈。
現(xiàn)在我們先來實(shí)現(xiàn)一個(gè)簡單的分頁組件,simple-pagination.component.ts
import { Component, Input, Output, EventEmitter } from '@angular/core'; @Component({
selector: 'simple-pagination',
template: `
<button (click)="previousPage()" [disabled]="!hasPrevious()">Previous</button>
<button (click)="nextPage()" [disabled]="!hasNext()">Next</button>
<p>page {{ page }} of {{ pageCount }} </p>
` }) export class SimplePaginationComponent { @Input() pageCount: number; @Input() page: number; @Output() pageChanged = new EventEmitter<number>();
nextPage() { this.pageChanged.emit(++this.page);
}
previousPage() { this.pageChanged.emit(--this.page);
}
hasPrevious(): boolean { return this.page > 1;
}
hasNext(): boolean { return this.page < this.pageCount;
}
}
app.component.ts
import { Component, OnInit } from '@angular/core'; import {ManagerService} from "./manager.service"; @Component({
selector: 'exe-app',
template: `
<simple-pagination
相關(guān)文章
如何處理Angular?錯(cuò)誤消息ERROR?Error?NullInjectorError?No?provid
這篇文章主要介紹了如何處理Angular?錯(cuò)誤消息ERROR?Error?NullInjectorError?No?provider?for?XX2023-07-07
angular2系列之路由轉(zhuǎn)場動(dòng)畫的示例代碼
本篇文章主要介紹了angular2系列之路由轉(zhuǎn)場動(dòng)畫的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Angularjs中$http以post請(qǐng)求通過消息體傳遞參數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Angularjs中$http以post請(qǐng)求通過消息體傳遞參數(shù)的方法,結(jié)合實(shí)例形式分析了$http使用post請(qǐng)求傳遞參數(shù)的相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2016-08-08
Angular4.0中引入laydate.js日期插件的方法教程
在AngularJs中我們會(huì)不可避免的使用第三方庫,例如jquery插件庫。下面這篇文章主要給大家介紹了關(guān)于Angular4.0中引入laydate.js日期插件的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Angularjs之如何在跨域請(qǐng)求中傳輸Cookie的方法
跨域傳輸Cookie是需要后臺(tái)和前臺(tái)同時(shí)做相關(guān)處理才能解決的,這篇文章主要介紹了Angularjs之如何在跨域請(qǐng)求中傳輸Cookie的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

