在Angular2中使用host-context时,重复选择器



我正在为身体元素使用汽车类,我想在angular2中使用它。

<body class="car">
  <my-app></my-app>
</body>

我尝试了:

:host-context(.car) h3 {
  background-color: blue
}

转换结果:

.car[_ngcontent-c42] h3[_ngcontent-c42], 
.car [_ngcontent-c42] h3[_ngcontent-c42]{
  background-color: blue
}

我想删除.car[_ngcontent-c42] h3[_ngcontent-c42]
我只需要这个类.car [_ngcontent-c42] h3[_ngcontent-c42]

我该怎么办?
谢谢。

ViewEncapsulation设置为None

@Component({
  selector: 'home',
  templateUrl: 'home.component.html',
  styles: [`
    .container {
      background: green;
    }
  `],
  encapsulation: ViewEncapsulation.None
})

您可以在此处阅读更多有关它的信息

您可以使用::ng-deep//deep/选择器

::ng-deep .car h3 {
  background-color: blue
}

要保持组件样式封装,请尝试以下操作:

.car :host-context h3 {
  background-color: blue
}

因此,您只能得到所需的东西:

.car [_ngcontent-c42] h3[_ngcontent-c42]

最新更新