Use NavigationLock on EditServer page to prevent user from navigating away before confirmation.
This commit is contained in:
parent
2da0063331
commit
ae3f096722
@ -1,9 +1,17 @@
|
|||||||
@page "/servers/{id:int}"
|
@page "/servers/{id:int}"
|
||||||
@* Route constraints: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-9.0#route-constraints *@
|
@* Route constraints: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-9.0#route-constraints *@
|
||||||
|
|
||||||
@attribute [ExcludeFromInteractiveRouting]
|
@* Note that this attribute now needs to be removed, because Routing is globally interactive.
|
||||||
|
The navigation manager only will respond correctly if all its behaviours are the same. *@
|
||||||
|
@* @attribute [ExcludeFromInteractiveRouting] *@
|
||||||
|
|
||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
|
@inject IJSRuntime JSRuntime
|
||||||
|
|
||||||
|
<NavigationLock
|
||||||
|
OnBeforeInternalNavigation="OnBeforeInternalNavigation"
|
||||||
|
ConfirmExternalNavigation="true">
|
||||||
|
</NavigationLock>
|
||||||
|
|
||||||
<h3>Edit server</h3>
|
<h3>Edit server</h3>
|
||||||
<br/>
|
<br/>
|
||||||
@ -68,4 +76,13 @@
|
|||||||
// An exception is raised when debugging from VS Code, but not when using dotnet watch.
|
// An exception is raised when debugging from VS Code, but not when using dotnet watch.
|
||||||
NavigationManager.NavigateTo("/servers");
|
NavigationManager.NavigateTo("/servers");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task OnBeforeInternalNavigation(LocationChangingContext context)
|
||||||
|
{
|
||||||
|
var isConfirmed = await JSRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to leave this page?");
|
||||||
|
if (!isConfirmed)
|
||||||
|
{
|
||||||
|
context.PreventNavigation();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user