angular - 特定组件的自定义样式应用于 Angular 6 中的所有组件
问题描述
ui 网格,我正在尝试将自定义样式应用于特定组件(我想更改该特定组件的字体大小),但是当我在该特定组件 css 文件中编写 css 代码时,并且在加载该样式被应用到的组件后所有其他组件也
以下是css文件中的代码
.k-grid td {
font-size: 10px !important;
}
.k-grid tr {
font-size: 10px;
}
ts文件中的代码
@Component({
selector: 'app-work-request-queue-child',
templateUrl: './work-request-queue-child.component.html',
styleUrls: ['./work-request-queue-child.component.css'],
encapsulation:ViewEncapsulation.None
})
以前的样式没有得到应用,所以在联系 Telerik 支持后,要求我在 ts 文件中添加封装:ViewEncapsulation.None。所以现在样式工作正常,但它被应用于所有组件,不知道为什么会发生。
解决方案
而不是使用encapsulation:ViewEncapsulation.None
您应该确保仅在加载组件时应用样式。
通过将以下内容添加到您的 CSS 中来做到这一点
:host ::ng-deep .k-grid td {
font-size: 10px !important;
}
:host ::ng-deep .k-grid tr {
font-size: 10px;
}
it will make sure that the style is applied, but only, in the context of your component being loaded.
even though the ng-deep
selector is marked as depreacated by angular, there is currently no better way to achieve this.
please also read the documentation about component styles and make sure to understand how it works: https://angular.io/guide/component-styles
推荐阅读
- angular - 使用 Angular 查询 Firebase 以获取文档密钥
- php - phpUnit测试elasticsearch结果为空
- ios - React Native 如何使用 expo 和 firebase 设置推送通知
- linux - 对 RHEL 6 使用 PowerCLI Invoke-VMScript 时,ifconfig 和 ping 命令不提供响应,为什么?
- ios - 如何验证对我的 Google 表格的查询?
- html - 我的下拉导航栏不会出现在文本前面
- python - 使用 Cython 实现 Numba 的性能
- regex - 使用正则表达式对多行进行编辑
- java - 运行 Jenkins 说“包 org.junit 不存在”
- python - 熊猫从最近的年末日期重新采样偏移量?