// card.component.ts
styleUrls: ['./card.component.scss']
// card.component.scss
.card {
padding: 10px;
box-shadow: 0 0 3px rgba(0,0,0,.2);
margin: 10px 0;
}
(2) 使用樣式字符串
styles: ['h5 { font-weight: normal; }']
還有一種最簡單粗暴的:
<app-card>
<h5 style="font-weight: normal">卡頭</h5>
</app-card>
注:雖然可以,但不推薦。app-card
,這就是宿主元素。app-card {
display: block;
margin: 10px 0;
background: #eee;
}
你會很郁悶的發(fā)現(xiàn)無效,咋辦呢?Angular還是很友好的,我們可以這樣:
:host {
display: block;
margin: 10px 0;
background: #eee;
}
這樣你會發(fā)現(xiàn)app-card
有背景色啦。display
屬性是空字符串的,也就是不占據(jù)空間。:host(.active) {
background: red;
}
:host-context(.theme-blue) {
color: blue;
}
注:它會在當前組件宿主元素的祖先節(jié)點中查找 CSS 類, 直到文檔的根節(jié)點為止。
強制給子組件加樣式
默認情況下,組件樣式只會作用于組件自身的HTML上。
我們先再創(chuàng)建一個組件,名為card-header
:
// card-header.component.html
<div class="card-header">
卡頭
</div>
然后在app.component.ts
中添加:
<app-card>
<app-card-header></app-card-header>
</app-card>
app/components/card/card.component.scss
中添加:
.card-header {
font-size: 25px;
}
上面的代碼對子組件card-header
里的內(nèi)容是不起作用的,只有這樣:
/deep/ .card-header {
font-size: 25px;
}
/deep/
就是用來強制一個樣式對各級子組件的視圖及其內(nèi)容都生效。<style>
div {color: #333;}
</style>
(2) link標簽
<link rel="stylesheet" href="app.css">
(3) import
@import 'app.css';
更多建議: