在Angular 9中创建一个可重用的警报框组件时,如果你发现更改警报类型后没有应用适当的类,这通常是因为组件的样式绑定或类绑定没有正确设置。以下是一些基础概念和解决方案:
假设你的警报框组件有一个输入属性alertType
,你可以使用ngClass
来根据这个属性的值动态绑定类名。
// alert-box.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-alert-box',
template: `
<div [ngClass]="alertType">
{{ message }}
</div>
`,
styles: [`
.success { color: green; }
.warning { color: orange; }
.error { color: red; }
.info { color: blue; }
`]
})
export class AlertBoxComponent {
@Input() alertType: string;
@Input() message: string;
}
在使用这个组件时,你可以这样绑定:
<app-alert-box [alertType]="'success'" message="操作成功!"></app-alert-box>
<app-alert-box [alertType]="'error'" message="发生错误!"></app-alert-box>
alertType
属性。alertType
属性是否正确传递到组件。通过以上步骤,你应该能够解决在Angular 9中更改警报类型时没有应用适当类的问题。如果问题仍然存在,建议检查Angular的变更检测机制是否正常工作,或者是否有其他CSS规则覆盖了你的样式。
领取专属 10元无门槛券
手把手带您无忧上云