css - 父组件的类在子组件中不起作用
问题描述
我有一个内部有 HTML 的组件,如下所示:
<div class="wrapper">
<lqs-myComponent class="my-component" [ngClass]="myComponentStatus ? 'is-active' : ''"></lqs-myComponent>
</div>
该组件的 CSS(或其中一些)是:
.wrapper {
.my-component {
display: grid;
width: 100%;
justify-self: center;
box-sizing: border-box;
border-radius: 5px;
row-gap: 5px;
align-self: start;
&.is-active {
> div {
transition: all 0.1s ease-in-out;
transform: translateY(0px);
@for $i from 0 through 50 {
&:nth-child(#{$i + 1}) {
transition-delay: (0.05s * $i) + 0.50s;
opacity: 1;
}
}
}
}
}
}
lqs-myComponent
基本思想是当is-active
页面上的某些点击事件应用时,div 内部会发生一些动画。所以该组件中的 div 类似于:
<div>Hello</div>
<div>Hello hello</div>
<div>Hello hello hello</div>
使用 CSS:
div {
transition: all 0.05s ease-in-out;
transform: translateY(20px);
opacity: 0;
}
我的问题是,上面的页面(带有wrapper
)是(click)
事件发生的地方,所以它也是is-active
应用类的地方。然而,实际的lqs-myComponent
组件,它只包含一堆 div(应该是动画的),好吧,它们根本没有动画。
我可以看到is-active
单击按钮时应用了该类,但我猜当该类应用于另一个组件时某些东西不能正常工作,但应该在另一个组件中工作。
我对 Angular 还很陌生,所以我现在不知道如何解决这个问题?
解决方案
在 Angular 中,组件 CSS 样式被封装到组件的视图中,并且不会影响应用程序的其余部分,要控制这种封装如何在每个组件的基础上发生,您可以在组件元数据中设置视图封装模式。
encapsulation: ViewEncapsulation.None
::ng-deep
或在您的课程中使用 deprecated
推荐阅读
- c# - 在 dotnet core web API 中使用现有 JSON 动态创建新 JSON 模型
- c++ - 检查先决条件应该是谁的责任?
- xamarin.forms - ItemsSource 更改时选择器未更新
- python - Python将文本文件中的数据加载到sqlite数据库中,并将表名和列作为参数传递
- mysql - MySQL LIMIT 在相关子查询中被忽略
- javascript - 是否引用了 JavaScript 闭包变量,或者它们是否包含自己的副本?
- java - 为自定义迭代器编写测试用例
- django - 如何以某种格式显示日期时间字段
- sql - 如何从两个相关表中获取数据并放入第三个表
- python - Python,AttributeError:“list”对象没有属性“get_model”