问题描述
从Blazor 3.1 Preview 2开始,应该可以阻止Blazor中链接的默认导航行为,也在此答案中讨论了.
Since Blazor 3.1 Preview 2 it should be possible to prevent default navigation behaviour for links in Blazor, as also discussed in this answer.
但是,此代码:
<NavLink href="" Match="Match" @onclick:preventDefault @onclick="()=>LinkAction()" >
Do something
</NavLink>
给出此错误:
那是为什么?
推荐答案
尽管HTML <A>
标签和Blazor NavLink
组件的最终结果大致相同,但@onclick:preventDefault
语法仅适用于HTML版本,不在Blazor组件上.
Although the net result of the HTML <A>
tag and the Blazor NavLink
component is the roughly the same thing, the @onclick:preventDefault
syntax only works for the HTML version, not on Blazor components.
Steve Sanderson 在此处进行解释:
Steve Sanderson explains this here:
史蒂夫也给出了可能的解决方案:
Steve also gives a possible solution:
@inherits NavLink
<a @attributes="@AdditionalAttributes" class="@CssClass" @onclick:preventDefault>
@ChildContent
</a>
现在,您可以使用而不是获取 您想要的行为.
Now you can use instead of to get the behavior you want.
这篇关于如何防止Blazor NavLink组件的默认导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!