是否可以使用条件构造在Blazor中启用属性? [英] Is it possible to use a conditional construct to enable an attribute in Blazor?
本文介绍了是否可以使用条件构造在Blazor中启用属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下剃刀组件.
@code
{
private bool isIndex = true;
}
@if (isIndex)
{
<NavLink href="" Match=NavLinkMatch.All>
Index
</NavLink>
}
else
{
<NavLink href="Other">
Other
</NavLink>
}
是否可以使用条件构造启用Match=NavLinkMatch.All
以呈现与上述相同的输出?
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>
推荐答案
是否可以使用条件构造启用
Match=NavLinkMatch.All
这是一个具有两个值的枚举.
It is an enum with two values.
您可以只使用Match="@(IsIndex ? NavLinkMatch.All : NavLink.Prefix)"
Prefix
是默认设置,因此您看不到太多.
You can just use Match="@(IsIndex ? NavLinkMatch.All : NavLink.Prefix)"
Prefix
is the default so you don't see it much.
但更笼统地说:不,您只能将C#逻辑应用于属性的 values .除非您想下拉至BuildRenderTree代码.
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.
这篇关于是否可以使用条件构造在Blazor中启用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文