问题描述
Blazor 有没有办法取消导航?
Is there a way on Blazor to cancel navigation?
让我们假设一个简单的 a href
像这样:
Let's supose a siple a href
like this:
<a href="/some/other/blazor/spa/page">Go</a>
我想取消导航(例如,如果我们正在编辑表单)
I would like to cancel navigation (for example if we are editing a form)
我通过 JSInterop 设置了 onbeforeunload
dom 委托:
I set onbeforeunload
dom delegate via JSInterop:
window.onbeforeunload = function(){
return 'Vols abandonar aquesta pàgina?';
};
但是 blazor 绕过它.
but blazor bypass it.
我尝试在 locationchanged
事件上拦截导航失败:
I tried unsuccessfully to intercept navigation on locationchanged
event:
@implements IDisposable
@inject NavigationManager NavigationManager
...
protected override void OnInitialized()
{
NavigationManager.LocationChanged += HandleLocationChanged;
}
private void HandleLocationChanged(object sender, LocationChangedEventArgs e)
{
// Cancel Navigation Here
}
public void Dispose()
{
NavigationManager.LocationChanged -= HandleLocationChanged;
}
推荐答案
目前没有,正在为 .NET V5 讨论此功能 - 并且有一个社区贡献请求请求,因此希望能够加入.https://github.com/dotnet/aspnetcore/pull/24417
Currently no, this feature is being discussed for .NET V5 - and has a community contribution pull-request so hopefully will make it in. https://github.com/dotnet/aspnetcore/pull/24417
在 Blazor 中导航时页面不会改变,因此没有卸载以触发 onbeforeunload.
The page is not changed when you navigate in Blazor, so there is no unload in order to trigger onbeforeunload.
这篇关于Blazor Navigationmanager 取消位置导航已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!