angular - 为什么这个组件没有应用 css 规则?
问题描述
我有一个名为 的组件app-list-container
,其结构如下:
HTML
<ul [attr.styleName]="styleName" [style.flexGrow]="flexGrow">
<!-- ITEM <li> -->
<ng-content></ng-content>
</ul>
SCSS
@import 'placeholders/scrollbar';
ul {
&[styleName='CHAT_MESSAGE'],
&[styleName='CHAT_LAST_MESSAGE'] {
@extend %scrollbar-y;
}
}
有了这个,我<li>
在这个组件中生成了项目()......现在我不明白为什么它不应用这个css
规则。
该规则仅在我将其插入他的父亲 ( app-list-container
) 时适用,对我来说,通过父亲也插入样式是没有意义的。
请注意,在下图中,css 正在应用于 html 元素,但如果应用于 html 元素,则它<ul>
不起作用,但如果应用于父元素( <app-list-containr>
),则它可以工作:
该怎么办?(注意:我喜欢做并且知道正确的模式)。
解决方案
对于那些想知道的人,我是这样做的:
TS:
export class ListContainerComponent implements OnInit {
@HostBinding('attr.styleName') hostStyleName: IListStyleName;
@Input() styleName: IListStyleName;
@Input() flexGrow = ICssFlexGrow.INITIAL;
constructor() {}
ngOnInit(): void {
this.checkInput();
this.setHostStyleName(this.styleName);
}
protected checkInput(): void {
if (!IListStyleName[this.styleName.toUpperCase()]) {
throw new Error(`The inserted style: "${this.styleName}" does not exist in this component!`);
}
if (!ICssFlexGrow[this.flexGrow.toUpperCase()]) {
throw new Error(`The inserted flex-grown: "${this.flexGrow}" does not exist in this component!`);
}
}
protected setHostStyleName(styleName: IListStyleName): void {
this.hostStyleName = styleName;
}
}
SCSS:
:host {
&[styleName='CHAT_MESSAGE'],
&[styleName='CHAT_LAST_MESSAGE'] {
@extend %scrollbar-y;
}
}
推荐阅读
- javascript - 按钮切换以显示抽屉模式弹出反应js
- kotlin - 发出通量项目时是否可以进行后续的 WebClient 调用?
- c++ - 使用指针比较数组中的元素 (C++)
- android - FFmpeg 仅在复制流时自动旋转视频
- operating-system - 吞吐量的操作系统计算
- c++ - 关于GLIBC的小内存应用缓存机制的问题。
- facebook - 在 Facebook 应用程序中打开特定帖子?
- java - 从 Java 中返回可选 ifPresent()
- javascript - 跟踪视频在网页中播放的时间
- html - 创建选项卡式图像库 - 更改我的 flexbox 卡