本篇內(nèi)容介紹了“Angular自定義組件添加默認(rèn)樣式”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)公司主要從事網(wǎng)站制作、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)涼山州,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
Angular的核心思想之一就是:組件化。組件化可以使我們的代碼更好的復(fù)用。
在使用官方提供的Angular庫Angular Material時(shí),細(xì)心的同學(xué)就會(huì)發(fā)現(xiàn),Material的每一個(gè)組件都有它自己樣式,如:
mat-buttonmat-toolbarmat-table每個(gè)組件添加自己獨(dú)有的樣式,增加css作用域的控制,實(shí)現(xiàn)了樣式的隔離。
那么,如果給一個(gè)自定義組件添加默認(rèn)樣式呢?接下來我們介紹三種方法來實(shí)現(xiàn)我們的目標(biāo)。
在組件的@Component裝飾器中提供了host屬性,該屬性可以為我們提供很多功能的支持,其中一項(xiàng)就是給組件添加樣式。
以Material中的Table為例:
@Component({
moduleId: module.id,
selector: 'mat-table, table[mat-table]',
exportAs: 'matTable',
template: CDK_TABLE_TEMPLATE,
styleUrls: ['table.css'],
host: {
'class': 'mat-table',
},
providers: [{provide: CdkTable, useExisting: MatTable}],
encapsulation: ViewEncapsulation.None,
// See note on CdkTable for explanation on why this uses the default change detection strategy.
// tslint:disable-next-line:validate-decorators
changeDetection: ChangeDetectionStrategy.Default,
})
export class MatTable<T> extends CdkTable<T> {
/** Overrides the sticky CSS class set by the `CdkTable`. */
protected stickyCssClass = 'mat-table-sticky';
}
在MatTable的源碼中,我們可以看到為host屬性設(shè)置了'class': 'mat-table',在我們使用MatTable組件時(shí),就會(huì)添加上默認(rèn)的樣式: mat-table.
注意
雖然在Angular中提供了host屬性,并且官方的Material庫也是使用該屬性實(shí)現(xiàn)了很多功能,但是,在Angular編碼規(guī)范中卻不推薦使用該方法。詳見:HostListener 和 HostBinding 裝飾器 vs. 組件元數(shù)據(jù) host
如方法一中注意事項(xiàng)中提到的,官方不推薦使用host屬性,推薦使用@HostBinding裝飾器來實(shí)現(xiàn)host的關(guān)于dom屬性相關(guān)的功能。
還是以MatTable為例,需要做一下改造來實(shí)現(xiàn)相應(yīng)的功能:
@Component({
moduleId: module.id,
selector: 'mat-table, table[mat-table]',
exportAs: 'matTable',
template: CDK_TABLE_TEMPLATE,
styleUrls: ['table.css'],
// host: {
// 'class': 'mat-table',
// },
providers: [{provide: CdkTable, useExisting: MatTable}],
encapsulation: ViewEncapsulation.None,
// See note on CdkTable for explanation on why this uses the default change detection strategy.
// tslint:disable-next-line:validate-decorators
changeDetection: ChangeDetectionStrategy.Default,
})
export class MatTable<T> extends CdkTable<T> {
/** Overrides the sticky CSS class set by the `CdkTable`. */
protected stickyCssClass = 'mat-table-sticky';
// 使用HostBinding裝飾器
@HostBinding('class.mat-table') clz = true;
}
Renderer2是Angular的渲染引擎,我們可以通過它來為自定義組件添加默認(rèn)樣式。
還是以MatTable為例,需要做一下改造來實(shí)現(xiàn)相應(yīng)的功能:
@Component({
moduleId: module.id,
selector: 'mat-table, table[mat-table]',
exportAs: 'matTable',
template: CDK_TABLE_TEMPLATE,
styleUrls: ['table.css'],
// host: {
// 'class': 'mat-table',
// },
providers: [{provide: CdkTable, useExisting: MatTable}],
encapsulation: ViewEncapsulation.None,
// See note on CdkTable for explanation on why this uses the default change detection strategy.
// tslint:disable-next-line:validate-decorators
changeDetection: ChangeDetectionStrategy.Default,
})
export class MatTable<T> extends CdkTable<T> {
/** Overrides the sticky CSS class set by the `CdkTable`. */
protected stickyCssClass = 'mat-table-sticky';
constructor(render: Renderer2, eleRef: ElementRef) {
render.addClass(eleRef.nativeElement, 'mat-table');
}
}
“Angular自定義組件添加默認(rèn)樣式”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前名稱:Angular自定義組件添加默認(rèn)樣式
文章地址:http://chinadenli.net/article18/pgpsgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、Google、做網(wǎng)站、動(dòng)態(tài)網(wǎng)站、網(wǎng)站維護(hù)、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)