导航后角度路由删除路由片段

时间:2019-09-06 07:19:07

标签: angular

导航到Route + Fragment后如何删除Angular路由片段?

在我当前的设置下,路由片段(http://address/#[fragment])成为导航URL的一部分,从而阻止了滚动到页面上不同部分的用户第二次使用路由+片段。这是因为URL已经处于“跳转到”状态。例如。 http://localhost:4200/home#shop

用户使用“跳转到”路线后,网址应从http://localhost:4200/ home#shop 改回http://localhost:4200/ home

我当前的代码

这是“跳至”路由器链接:

<a [routerLink]="['/home']" fragment="shop">shop</a>

在我的routing-module.ts中:

imports: [RouterModule.forRoot(routes, {
    scrollPositionRestoration: 'enabled',
    anchorScrolling: 'enabled',
    scrollOffset: [0, 64]
})],

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试使用@angular/common中的位置信息。

import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';

constructor(
   private location: Location
) { }

ngOnInit() {
   // replaceState(path: string, query: string = '', state: any = null): void
   this.location.replaceState(pathWithoutHash);
}

您还可以稍等片刻,然后将其超时删除。

The docs