這篇文章主要為大家展示了“Angular5如何給組件本身的標簽添加樣式”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Angular5如何給組件本身的標簽添加樣式”這篇文章吧。
在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 {}以上是“Angular5如何給組件本身的標簽添加樣式”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。