css - 在将影响整个 css 角度 6 的组件中应用 css 类
问题描述
我有一个组件
my.component.ts 像这样有自己的css
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: './my.component.html',
styleUrls: ['./my.component.css']
})
export class NotificationComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
我的.component.css
.hello{
color:red;
}
问题是我想使用该类在 html 中的整个项目中可见,我知道我可以将它们放在全局范围内,但我想从组件中使用它。
现在其他组件看不到该类:(
Angular中有某种解决方案吗?
解决方案
请查看有关View Encapsulation的文档,尤其是“无”部分:
None 意味着 Angular 没有视图封装。Angular 将 CSS 添加到全局样式中。前面讨论的范围规则、隔离和保护不适用。这本质上与将组件的样式粘贴到 HTML 中相同。
将其应用于组件装饰器配置对象:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
encapsulation: ViewEncapsulation.None // <-- This
})
推荐阅读
- firebase - 使用 Google 和 Firebase 反应原生身份验证
- r - 如何避免 ggplot 在 R 中的因子内分组
- lua - 如何获得最近的玩家(FiveM Lua)
- postgresql - Postgres StatefulSet 已成功创建 - 但尚未准备好
- c# - 在 Xamarin.Forms 中将 StringFormat 1000000 标记为 1.000.000
- laravel - 如何在 laravel 5.6 的 with() 块中使用 Eloquent 列名?
- sql - 与普通查询相比,Oracle 存储过程需要更多时间
- django - AttributeError:“ModelChoiceField”对象没有属性“规范”
- c++ - 如何捕获 C++ 内置异常对象
- azure-cloud-shell - Azure 云外壳:下载命令不起作用