简体   繁体   English

blazor中的三元运算,将一条if-else语句转化为一条语句

[英]ternary operation in blazor, convert a if-else statement into just one statement

I have the following code in a page.razor:我在 page.razor 中有以下代码:

                    @if (i==1)
                    {
                        <MudTimelineItem  Color="Color.Primary" Size="Size.Medium" Variant="Variant.Outlined">
                            <MudAlert Severity="Severity.Success">@matrix.UserName</MudAlert>
                        </MudTimelineItem>
                    }
                    else
                    {
                        <MudTimelineItem   Variant="Variant.Outlined">
                            <MudAlert Severity="Severity.Success">@matrix.UserName</MudAlert>
                        </MudTimelineItem>
                    }

The only thing that changes is the color and size parameters, rest remains the same, Instead of using if-else statement can this be written in just one line using ternary operators or any other with which blazor supports?唯一改变的是颜色和大小参数,rest 保持不变,而不是使用 if-else 语句,可以使用三元运算符或 blazor 支持的任何其他运算符将其仅写在一行中吗?

Blazor should not render attribute if it's value is null or false ( docs ).如果 Blazor 的值为nullfalse ( docs ),则不应呈现该属性。 Try something like:尝试类似的东西:

<MudTimelineItem Color="@(i == 1 ? Color.Primary : null)" Size="@(i == 1 ? Size.Medium : null)" Variant="Variant.Outlined">
    <MudAlert Severity="Severity.Success">@matrix.UserName</MudAlert>
</MudTimelineItem>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM