[英]Parent class is not inherited by the descendant element in the child component-using scss
我有一個父組件,其父類是通過指令設置的(我根據用戶輸入給我一個主題)。
在子組件的SCSS
樣式中,即使引用父類的最簡單的規則也無效。 我閱讀了有關陰影穿刺的ViewEncapsulation
,並在父組件ViewEncapsulation
設置為None
,但是由於我確定有些ViewEncapsulation
而無法使它正常工作。
app.component
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
}
app.component.html
<div appTheme> <---- This one generates: class="theme-snow-cherry" (for example)
<router-outlet></router-outlet>
</div>
ribbon.component.scss (子組件)
.theme-snow-cherry div {
background-color: red;
}
注意:如果我在app.component.css
添加上述樣式,它將為我提供結果,但顯然不是我要放置樣式的地方。
由於您是在子組件中定義樣式的,因此應在該組件中將封裝設置為ViewEncapsulation.None
:
@Component({
...
encapsulation: ViewEncapsulation.None
})
export class RibbonComponent {
...
}
否則,生成的類樣式具有特定於子組件的屬性,這會阻止選擇器.theme-snow-cherry
與父組件中的div元素匹配:
.theme-snow-cherry[_ngcontent-c2] div[_ngcontent-c2] {
background-color: red;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.