Snu*_*den 3 c# blazor blazor-webassembly
如何检测 URL 何时更改?我需要事件(URL 更改事件)来根据 URL 执行某些特定操作。
您可以使用NavigationManager
它具有内置LocationChanged
事件。
要处理该事件,您可以像这样注入 NavigationManager:
@inject NavigationManager nav
Run Code Online (Sandbox Code Playgroud)
要显示警报,您可能还需要添加 JSRuntime:
@inject IJSRuntime js
Run Code Online (Sandbox Code Playgroud)
并通过覆盖订阅事件OnInitialize()
protected override void OnInitialized()
{
nav.LocationChanged += (o, e) => {
js.InvokeVoidAsync("alert", "Alert"); // when location is changed invoke alert js function
};
}
Run Code Online (Sandbox Code Playgroud)
您可以在此链接中检查此示例的 repl:https ://blazorrepl.com/repl/wabkuybC54SQD88I53