我正在把一个项目从Angular迁移到Blazor。. NET 6 rc2),我很好奇Blazor是否有类似的复合样式能力。
在Angular中,你可以将值绑定到style
字符串的部分,并且该字符串将在运行时被构造。例如
<div [style.padding-bottom.%]="displayAspectRatio">
</div>
将读取我的代码属性displayAspectRatio
并将其组成元素的样式字符串,例如style="padding-bottom: 32.8%;"
我总是在可能的地方使用类,但是在那些特殊的情况下,样式必须在每个元素的基础上计算,在Blazor目前的最佳实践是什么?在Blazor .NET6中它仍然是;
<div style="@($"padding-bottom: {displayAspectRatio}%")">
</div>
没有直接等价的。唯一的方法就是你提到的方法。但是,如果属性太多,您可以使用属性飞溅或任意参数。
https://learn.microsoft.com/en us/aspnet/core/blazor/components/?view=aspnetcore - 5.0 # attribute-splatting-and-arbitrary-parameters-1