Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting navigation events blazor webassembly [closed]

How can I detect when URL is changed? I need the event (URL change event) to perform some specific action based on the URL.

like image 913
Snuden Avatar asked Jan 20 '26 00:01

Snuden


1 Answers

You can use NavigationManager which has built-in LocationChanged event.

To handle the event you can inject the NavigationManager like this:

@inject NavigationManager nav

To show the alert you may need to add the JSRuntime too:

@inject IJSRuntime js

and subscribe to the event with overriding OnInitialize()

protected override void OnInitialized()
{
    nav.LocationChanged += (o, e) => {
        js.InvokeVoidAsync("alert", "Alert"); // when location is changed invoke alert js function
    };
} 

You can check the repl of this example in this link: https://blazorrepl.com/repl/wabkuybC54SQD88I53

like image 52
Kristian.mariyanov Avatar answered Jan 21 '26 22:01

Kristian.mariyanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!