Angular 5 编译期间未拾取 CSS 类

标签 css angular sass ag-grid

我在我的 Angular 5 元素中使用 ag-grid。我正在尝试将 css 类添加到单元格中。在查看了有关 ag-grid 单元样式的大量文档后,我尝试使用 cellClass 和 cellClassRules。

样式在scss文件中定义,例如:

.readonly-cell{ background-color: #cccccc; }

然后将 scss 文件包含在组件中:

@Component({
  selector: 'app-volume',
  templateUrl: './volume.component.html',
  styleUrls: ['./volume.component.scss']
})

然后我将类应用到列:

{headerName:'Power', field:'power', width:150, cellClass:'readonly-cell'}

网格本身工作正常。问题是power 电池不会改变颜色。当我在 Firefox 中检查渲染的 HTML 时,我可以看到单元格确实应用了类 readonly-cell 。但样式详细信息未在规则面板上列出。

这让我认为这些类在编译过程中没有被拾取。我不认为这是 ag-grid 的问题,而是选择样式类的方式的问题。

有什么方法可以解决为什么类和定义没有包含在编译中的问题吗?

最佳答案

您会得到这种行为,因为您尝试通过 CSS 规则定位的元素是在 Angular 组件外部生成的,并且 Angular 添加了特殊属性,以便组件 CSS 仅适用于该组件(而不适用于其子组件、添加的任何 DOM 节点) Angular 之外等)。您应该使用 Angular 并在与您的样式完全相同的组件中构建样式所需的所有 HTML,或者禁用该功能。您可以使用 ViewEncapsulation.None 禁用它:

@Component({
  selector: 'app-volume',
  templateUrl: './volume.component.html',
  styleUrls: ['./volume.component.scss'],
  encapsulation: ViewEncapsulation.None
})

或者在样式表中使用 /deep/,如下所述:

https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep

关于Angular 5 编译期间未拾取 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51173268/

相关文章:

angular - 如何使用 StackBlitz 编辑器对 TypeScript 模块进行 "Import"

javascript - 检查单选按钮是否脏/以 react 形式被触摸

angular - 找不到 'ramda' 的类型定义文件

css - Errno 2 No Such file or directory Sass 没有在 sublime text 2 中编译

html - 带有网格模板区域的网格布局

css - 如何在当前目录中为单个文件编译 node-sass

javascript - toggleClass 不保留以前的点击事件 jquery

html - 将页脚框放在内容框后面

html - 是否可以在 Bootstrap 中具有垂直响应功能?

html - Angular Material 将图像放在 <mat-select> 的选定值上