如何使用Angular Router / Location清除路由历史记录返回特定页面

问题描述 投票:0回答:1

通过清除路由历史回到主页是我的要求

我试过下面的代码。但是下面显示的所有代码都不符合我的要求。

this.router.navigate(['/services/paymentProcessing'], { skipLocationChange: true });

ngOnDestroy(): void {
    window.history.go(-4);
    this.location.back();
    this.router.navigate(['/services/#SH']);
  }

我需要通过清除路由历史记录从我的特定页面返回主页/特定页面。任何人都可以让我知道解决方案

angular angular-routing angular-router
1个回答
0
投票

通过参考How to Detect Browser Back Button event - Cross BrowserHow do I detect user navigating back in Angular2?的答案,我希望让您知道上述任务的解决方案。

要返回特定页面,通过跳过当前页面,您可以使用该代码

this.router.navigate(["/C"], { replaceUrl: true });

正如Stark Buttowski先生所说

然后,如果您希望保留当前页面(在我的情况下为HomePage)而不清除历史记录,

import { Location } from "@angular/common";

constructor(public location: Location){}

ngOnDestroy(): void {
    this.location.subscribe(
      x=> history.pushState(null, null, window.location.pathname)
    );
  } 

这样你就可以阻止按钮导航了。

如果有,任何更好的代码行,期待..

寻找答案,这比上面好

© www.soinside.com 2019 - 2024. All rights reserved.