我有以下html布局。
https://stackblitz.com/edit/angular-rfiey8?file=src%2Fapp%2Fapp.component.css
我需要在parent
类上设置display:flex
和flex-wrap:wrap
,这样输入将始终在左边的数字旁边。
但是当我把数字html布局在单独的组件
https://stackblitz.com/edit/angular-g53hgs?file=src%2Fapp%2Fapp.component.css
我设法通过:host ::ng-deep
选择器从父组件样式化HTML。
.parent {
display: flex;
flex-wrap: wrap;
}
不像在第一个例子中,HTML不在组件本身。
我被困在这很长时间。我怎么能使display:flex
在父类是相等的,就像在第一个例子?
将parent
css类移动到组件标签上,像这样
<div>
<app-numbers class="parent" [arr]="arr"></app-numbers>
<input />
</div>
如果你已经有了一个单独的number组件,那么将.number
样式移动到该组件的css文件中,不需要使用ng-deep
移动css到number组件
https://stackblitz.com/edit/angular-jlkeea
number.component.css
:host {
display: flex;
flex-wrap: wrap;
}
.number {
width: 20px;
background-color: blue;
border-radius: 20px;
height: 20px;
text-align: center;
padding: 10px;
margin: 5px;
}
app.componet.css
.parent {
display: flex;
}
input {
flex: 1;
}
这样就不会破坏视图封装