如何做网站排名,合肥全网优化,电商网站开发用什么语言表达,佛山网站建设首选原文链接#xff1a;https://blazor-university.com/routing/detecting-navigation-events/检测导航事件源代码[1]从 Blazor 访问浏览器导航是通过 NavigationManager 服务提供的。这可以使用 razor 文件中的 inject 或 CS 文件中的 [Inject] 属性注入到 Blazor 组件中。Locat… 原文链接https://blazor-university.com/routing/detecting-navigation-events/检测导航事件源代码[1]从 Blazor 访问浏览器导航是通过 NavigationManager 服务提供的。这可以使用 razor 文件中的 inject 或 CS 文件中的 [Inject] 属性注入到 Blazor 组件中。LocationChanged 事件LocationChanged 是在浏览器中的 URL 发生更改时触发的事件。它传递一个 LocationChangedEventArgs 实例该实例提供以下信息public readonly struct LocationChangedEventArgs
{public string Location { get; }public bool IsNavigationIntercepted { get; }
}Location 属性是显示在浏览器中的完整 URL包括协议、路径和任何查询字符串。IsNavigationIntercepted 指示导航是通过代码还是通过 HTML 导航启动的。false导航由从代码调用的 NavigationManager.NavigateTo 启动。true用户单击 HTML 导航元素例如 a hrefBlazor 拦截导航而不是允许浏览器实际导航到新 URL这将导致向服务器发出请求。在其他情况下也是如此例如页面上的某些 JavaScript 导致导航例如在超时之后。最终任何不是通过 NavigationManager.NavigateTo 发起的导航事件都将被视为拦截导航并且该值为 true。请注意目前无法拦截导航并阻止其继续进行。观察 OnLocationChanged 事件需要注意的是NavigationManager 服务是一个长期存在的实例。因此任何订阅其 LocationChanged 事件的组件都将在服务的生命周期内被强引用。因此重要的是我们的组件在它们被销毁时也取消订阅此事件否则它们将不会被垃圾收集。目前ComponentBase 类在销毁时没有生命周期事件但可以实现 IDisposable 接口。implement IDisposable
inject NavigationManager NavigationManagerprotected override void OnInitialized()
{// Subscribe to the eventNavigationManager.LocationChanged LocationChanged;base.OnInitialized();
}void LocationChanged(object sender, LocationChangedEventArgs e)
{string navigationMethod e.IsNavigationIntercepted ? HTML : code;System.Diagnostics.Debug.WriteLine($Notified of navigation via {navigationMethod} to {e.Location});
}void IDisposable.Dispose()
{// Unsubscribe from the event when our component is disposedNavigationManager.LocationChanged - LocationChanged;
}参考资料[1]源代码: https://github.com/mrpmorris/blazor-university/tree/master/src/Routing/NavigatingViaCode