【发布时间】:2019-08-09 12:24:25
【问题描述】:
我需要从 Angular 组件中访问生成的 css 类名,以便为第 3 方组件设置样式。
Angular 对本地 css 类名进行了一些神奇的转换以启用范围。我需要将一些自定义样式应用于ngx-datatable 组件。为此,我需要将自定义类名传递给它。由于 Angular 对类名的作用,这些不再匹配。
将类名添加到全局范围或使用::ng-deep 都可以,但是我不想破坏封装。
dashboard-component.ts
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent {
getRowClass(row){
return 'my-class';
}
}
dashboard-component.scss
.my-class {
background: green;
}
dashboard-component.html
<ngx-datatable
[rowclass]="getRowClass"
></ngx-datatable>
按照我的看法,我应该能够从组件中访问一些对 css 类的引用,比如 this._styles,然后它将在运行时携带生成的类的名称,所以我可以这样做
getRowClass(row){
return this._styles['my-class'];
}
【问题讨论】:
-
你可以设置
encapsulation to ViewEncapsulation.None