在Angular 5給組件本身的標簽添加樣式有兩種方法:
方式一:使用@Component的host屬性
@Component({ selector : 'myComponent', host : { '[style.color]' : "'red'", '[style.background-color]' : 'backgroundColor' } }) class MyComponent { backgroundColor: string; constructor() { this.backgroundColor = 'blue'; } }
在host配置里添加屬性,等同于標簽上綁定屬性的用法一樣。
設置style:
'[style.color]': "'red'":注意red值雙引號里還有一個單引號。
'[style.background-color]':'backgroundColor':這里是引用了組件里的變量backgroudColor。
這種方式的好處是可以在樣式上使用組件的變量。
設置class:
@Component({ selector : 'myComponent', host : { '[class.myclass]' : 'showMyClass' } }) class MyComponent { showMyClass = false; constructor() { } toggleMyClass() { this.showMyClass = !this.showMyClass; } }
方式二:在樣式里使用:host選擇器
@Component({ selector : 'myComponent', styles : [` :host { color: red; background-color: blue; } `] }) class MyComponent {}
相信看了本文案例你已經掌握了方法,更多精彩請關注Gxl網其它相關文章!
推薦閱讀:
React Router v4使用詳解
vue表單入門使用須知
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com