[英]Disabling effect of ViewEncapsulation.None in Angular
How to disable the effect of ViewEncapsulation.None? 如何禁用ViewEncapsulation.None的效果? Eg One of my component (firstComponent) defines a css class with some properties. 例如,我的一个组件(firstComponent)定义了具有某些属性的CSS类。 There is secondComponent which uses the same css class. 有secondComponent使用相同的CSS类。 I want my "secondComponent" to use the different specific values for properties defined by first component stylesheet. 我希望我的“ secondComponent”对第一个组件样式表定义的属性使用不同的特定值。 How can I achieve this? 我该如何实现?
Note : I redefined the same class in "secondComponent" with different values, keeping the viewEncapsulation of secondComponent default. 注意:我在“ secondComponent”中用不同的值重新定义了相同的类,并保持secondComponent的viewEncapsulation为默认值。 It didn't work for me. 它对我不起作用。
FirstComponent:
@Component({
moduleId: module.id,
selector: "FirstComponent",
templateUrl: 'FirstComponent.component.html',
styleUrls: ['FirstComponent.component.css'],
encapsulation: ViewEncapsulation.None
})
FirstComponent.component.css
.ui-tree .ui-tree-container {
background-color: #252525;
color: white;
}
Second Component:
@Component({
moduleId: module.id,
selector: "SecondComponent",
templateUrl: 'SecondComponent.component.html',
styleUrls: ['SecondComponent.component.css'],
})
SecondComponent.Component.css
.ui-tree .ui-tree-container {
background-color: white;
color: black;
}
I am creating p-tree in both the component, which internally uses .ui-tree-container. 我在两个组件中都创建了p-tree,内部使用了.ui-tree-container。 I want my secondComponent's tree's background should be white, while for all other places tree's background should remain black. 我希望我的secondComponent的树背景应该是白色,而在所有其他地方树的背景应该保持黑色。
You can encapsulate your css within your component selectors. 您可以将CSS封装在组件选择器中。
FirstComponent.component.css
FirstComponent .ui-tree .ui-tree-container {
background-color: #252525;
color: white;
}
SecondComponent.component.css
SecondComponent .ui-tree .ui-tree-container {
background-color: white;
color: black;
}
With this way, they will not affect each other templates. 这样,它们将不会互相影响模板。 Also, you can choose to use ViewEncapsulation.None
for both/either of them or not. 另外,您可以选择对两个/一个都使用ViewEncapsulation.None
。
You can use Default ViewEncapsulation for the FirstComponent as well, and instead use ::ng-deep selector in your css files respectively. 您也可以对FirstComponent使用Default ViewEncapsulation,而在CSS文件中分别使用:: ng-deep选择器。
SecondComponent SecondComponent
::ng-deep .ui-tree .ui-tree-container{
background-color: white;
color: black;
}
FirstComponent FirstComponent
::ng-deep .ui-tree .ui-tree-container{
background-color: #252525;
color: white;
}
If you want to reuse CSS white only customising somes parts, you could make use of scss variables 如果只想重用CSS白色来定制部分,则可以使用scss变量
Step #1 第1步
Create a scss file with common properties and default colors 创建具有通用属性和默认颜色的scss文件
commontree.scss commontree.scss
$bgColor : white !default;
$color: black !default;
.ui-tree .ui-tree-container {
background-color: $bgColor;
color: $color;
}
Step #2 第2步
In your component's scss file, modify the variables' values if needed and import the comon scss file 在组件的scss文件中,根据需要修改变量的值,然后导入通用的scss文件
component1.scsss component1.scsss
$bgColor: #252525; /* Override colors */
$color: white;
@import './commontree.scss';
component2.scss component2.scss
/* Use default colors */
@import './commontree.scss';
For this, you can (and probably should) keep view encapsulation to the default ViewEncapsulation.Emulated
为此,您可以(可能应该)将视图封装保持为默认的ViewEncapsulation.Emulated
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.