How do you detect navigation events in Blazor WebAssembly?

Platform: Blazor| Category : Event handling, General

The LocationChanged event handler is triggered when there is a change in the navigation location. In the example below, a JavaScript interop function is used to display an alert to the user whenever the navigation location changes:

@inject NavigationManager UriHelper
@inject IJSRuntime JSRuntime 

<p> When navigating to another page, an alert box will be displayed to indicate the navigation. </p> 

@code { 
    protected override void OnInitialized ()   
    { 
        UriHelper.LocationChanged += DetectNavigation; 
    } 

    private void DetectNavigation ( object sender, LocationChangedEventArgs e ) 
    { 
        JSRuntime.InvokeVoidAsync("alert", "Navigation event is triggered"); 
    } 
} 

Refer to this documentation for more details. 

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.