<A>
和 Blazor 的 NavLink
组件的净结果大致相同,但 @onclick:preventDefault
语法仅适用于 HTML 版本,而不适用于 Blazor 组件。Steve 还提供了一个可能的解决方案:恐怕没有传递任意指令属性(例如 @*:preventDefault)作为组件参数的机制,因此这不应该在 NavLink 上起作用。
However, you could inherit your own subclass from NavLink that adds the "prevent default" behavior. For example, create NavLinkPreventDefault.razor, containing this:
@inherits NavLink <a @attributes="@AdditionalAttributes" class="@CssClass" @onclick:preventDefault> @ChildContent </a>
Now you can use instead of to get the behavior you want.
<NavLink class="nav-link" Match="NavLinkMatch.Prefix" @onclick="() => SetToActive(item)">
<span class="pcoded-micon">
<b class="fa-custom fas fa-bars @item.Icon"></b>
</span>
<span class="pcoded-mtext">@item.Title</span>
</NavLink>
private void SetToActive(CoolModel item)
{
// do something...
if (item.Route is not null)
{
_navigationManager.NavigateTo(item.Route);
}
}
@inherits NavLink <a @attributes="@AdditionalAttributes" class="@CssClass" @onclick:preventDefault="OnClickPreventDefault">@ChildContent</a> @code { [Parameter] public bool OnClickPreventDefault { get; set; } }
- Moo