gpt4 book ai didi

c# - 如何防止 Blazor NavLink 组件的默认导航

转载 作者:行者123 更新时间:2023-12-04 12:16:47 26 4
gpt4 key购买 nike

从 Blazor 3.1 Preview 2 开始,应该可以 prevent default navigation behaviour对于 Blazor 中的链接,还有 discussed in this answer .

但是,这段代码:

<NavLink href="" Match="Match" @onclick:preventDefault @onclick="()=>LinkAction()" >
Do something
</NavLink>

给出这个错误:

The component parameter 'onclick' is used two or more times for this component. Parameters must be unique (case-insensitive)



这是为什么?

最佳答案

尽管 HTML 的最终结果 <A>标签和 Blazor NavLink组件大致相同,@onclick:preventDefault语法仅适用于 HTML 版本,不适用于 Blazor 组件。

史蒂夫·桑德森 explains this here :

I'm afraid there isn't a mechanism for passing through arbitrary directive attributes such as @*:preventDefault as component parameters, so this isn't expected to work on NavLink.



Steve 还给出了一个可能的解决方案:

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.

关于c# - 如何防止 Blazor NavLink 组件的默认导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60653318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com