得到了家庭组件background-color:red
写入其 scss,然后获取用户组件background-color:green
写入其 scss 中。我启动我的应用程序,我在家,有红色背景,转到用户页面,有绿色背景。工作正常……但现在当我返回时,我的主页组件背景仍然是绿色的。所有组件都有ViewEncapsulation.None
。
如果我从用户页面开始导航,也会发生同样的事情,但背景颜色是vica-vera。
我一直理解组件样式的要点是仅影响其组件而不影响其他组件。这不应该是这样的吗?
编辑:如果我设置ViewEncapsulation.Emulated
我看到没有应用组件样式 scss 文件的样式,因此两个页面都有白色背景。
这是我的主组件文件的样子:
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
encapsulation: ViewEncapsulation.Emulated,
})
export class HomeComponent implements OnInit {
ngOnInit() {
}
}
编辑:你看我的问题是我正在设置背景颜色<body>
,但是主体不是模板的一部分,这就是为什么encapsulation: ViewEncapsulation.Emulated
并且组件样式表不会影响它。
您需要将您的视图封装到Emulated
,这样你的组件装饰器将如下所示 -
@Component({
// ...
encapsulation: ViewEncapsulation.Emulated,
})
它将仅将您的样式范围限定到特定组件。
Here https://angular.io/guide/view-encapsulation关于View Encapsulation的更多参考
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)