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

岁酱吖の 提交于 2020-08-10 20:20:06

问题


Consider the following Razor component.

@code
{
    private bool isIndex = true;
}


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

Is it possible to use a conditional construct to enable Match=NavLinkMatch.All that renders the same output as above?

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

回答1:


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

It is an enum with two values.

You can just use Match="@(IsIndex ? NavLinkMatch.All : NavLink.Prefix)"
Prefix is the default so you don't see it much.

But more in general: no, you can only apply C# logic to the values of attributes. Unless you want to drop down to BuildRenderTree code.



来源:https://stackoverflow.com/questions/62682426/is-it-possible-to-use-a-conditional-construct-to-enable-an-attribute-in-blazor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!