是否可以使用条件构造来启用 Blazor 中的属性?

Is it possible to use a conditional construct to enable an attribute in Blazor?

考虑以下 Razor 组件。

@code
{
    private bool isIndex = true;
}


@if (isIndex)
{
    <NavLink href="" Match=NavLinkMatch.All>
        Index
    </NavLink>
}
else
{
    <NavLink href="Other">
        Other
    </NavLink>
}

是否可以使用条件构造来启用 Match=NavLinkMatch.All 以呈现与上述相同的输出?

<NavLink href=@(isIndex? string.Empty:"Other")>
    @(isIndex? "Index": "Other")
</NavLink>

Is it possible to use a conditional construct to enable Match=NavLinkMatch.All

这是一个有两个值的枚举。

你可以直接使用Match="@(IsIndex ? NavLinkMatch.All : NavLink.Prefix)"
Prefix 是默认设置,所以您看不到它。

但更一般地说:不,您只能将 C# 逻辑应用于属性的 。除非你想下拉到 BuildRenderTree 代码。