使用:host-context()在不同样式之间切换
通过应用不同的(预定义的类或属性)切换
:host-context(.class1) {
  background-color: red;
}
:host-context(.class2) {
  background-color: blue;
}
<my-comp class="class1></my-comp> <!-- should be red -->
<my-comp class="class2></my-comp> <!-- should be blue -->
使用全局样式
* /deep/ my-comp.class1 {
  background-color: red;
}
// or to style something inside the component
* /deep/ my-comp.class1 /*deep*/ div {
  border: solid 3px yellow;
}
* /deep/ my-comp.class2 {
  background-color: blue;
}
使用主机绑定
@Component({
  selector: 'my-comp',
  host: {'[style.background-color]':'backgroundColor'}
})
class MyComponent {
  @Input() backgroundColor:string;
}
<my-comp background-color="red"></my-comp>
<my-comp background-color="red"></my-comp>
另请参阅/sf/answers/2555255881/以获取有趣的"黑客".
| 归档时间: | 
 | 
| 查看次数: | 1675 次 | 
| 最近记录: |