Angular5 项目中遇到:当我在某一个组件的css文件中使用css类选择器去选择组件中的插件元素,发现选择器选择不到该插件元素。css
通过google分析发现是组件中的一个属性在做祟: encapsulation. 默认状况下: angular组件对应的css只在该组件下起做用,对于不属于angular组件的元素是不起做用的(例如组件中引入的各个插件 highcharts tooltips bootstrap
)。
html
想要解决这个问题,咱们须要设定css为非包裹性的,即css选择器的范围是body内的全部元素,而非只是当前的组件。bootstrap
code:api
1 import { Component, OnInit, OnDestroy, ViewChild, Input, ViewEncapsulation, ElementRef, AfterViewInit, HostListener, ViewChildren, QueryList, Renderer2 } from '@angular/core'; 2 ....... 3 import { BsModalService } from 'ngx-bootstrap'; 4 const highchartsWordcloud = require('highcharts'); 5 require('highcharts/modules/wordcloud')(highchartsWordcloud); 6 7 @Component({ 8 templateUrl: './individual-group.component.html', 9 styleUrls: ['./individual-group.component.scss'], 10 encapsulation: ViewEncapsulation.None 11 })
ViewEncapsulation是一个枚举类型,有四个值,具体可参考官方文档ide
If encapsulation is not supplied, the value is taken from CompilerOptions
. The default compiler option isViewEncapsulation.Emulated
.ui
下面这篇文章是我以为讲解 encapsulation比较详细的,你们能够作参考google