【发布时间】:2023-03-30 17:52:01
【问题描述】:
我有一个 Angular 组件,定义如下:
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'rdc-dynamic-icon-button',
templateUrl: './dynamic-icon-button.component.html',
styleUrls: ['./dynamic-icon-button.component.scss']
})
export class DynamicIconButtonComponent {
@Input() label = '';
@Input() icon = '';
@Input() fileType = '';
@Output() click = new EventEmitter<any>();
onClick() {
this.click.emit();
}
}
这是组件模板:
<button (click)="onClick()">
<img src="../../../assets/icons/{{icon}}.{{fileType}}" />
<span class="buttonLabel">{{ label }}</span>
</button>
这是 CSS:
button {
font-size: 0.8em;
width: 150px;
height: 45px;
background-color: white;
color: #0066cc;
border: 1px solid #0066cc;
border-radius: 30px;
padding: 1em;
}
// The button's icon
img {
padding-right: 0.5em;
position: relative;
bottom: 5px;
}
为了让左侧的图标与按钮标签正确对齐,我使用了 CSS 属性 bottom: 5px。但是由于这个按钮本质上是动态的,当然这个 CSS 规则将始终被使用,无论哪个图标最终被传递给组件。
在截图中,你可以看到右边按钮中的心形图标被bottom: 5px推得太远了。
如何仅更改第二个按钮的 CSS 属性以更好地垂直放置心形图标?这是模板:
<rdc-dynamic-icon-button
label="Share this page"
icon="share_icon"
fileType="svg"
class="results-descr-button1"
></rdc-dynamic-icon-button>
<rdc-dynamic-icon-button
label="Save this page"
icon="fill-1"
fileType="svg"
class="results-descr-button2"
></rdc-dynamic-icon-button>
一位同事建议使用 [ngStyle],但从我在谷歌上看到的情况来看,这仅在选择特定 HTML 标记时有用,不能用于在 Angular 组件中选择 CSS 选择器inside。我可能错了。
【问题讨论】:
-
您是否可以控制组件中显示的图标的大小(宽度/高度,而不是字节)?
-
它们是 SVG,所以是的。