如何清除 Angular 路由器中的导航历史记录

She*_*har 5 angular

我有一个用例,其中我需要使用清除导航历史记录angular-router(以便按下硬件后退按钮不会将我带到上一页,而是主页)。

我一直在寻找一个简单的解决方案,但还没有找到。

我有使用建议nativescript,但我不想增加我的包大小以满足此要求。

angular请告知我是否可以使用任何内置功能?

小智 10

您可以通过两种方式做到这一点。如果您不需要历史记录中的状态,那么您可以使用如下内容:

this.router.navigate([`/myPage/`], { relativeTo: this.route, skipLocationChange: true });
Run Code Online (Sandbox Code Playgroud)

或者,如果您只想用新状态替换历史记录中的当前状态:

this.router.navigate(['/home'], {replaceUrl: true});
Run Code Online (Sandbox Code Playgroud)


She*_*har 1

我能够使其工作的唯一方法是使用 NavContoller

   this.nav.navigateRoot('/tabs/home');
Run Code Online (Sandbox Code Playgroud)

这从堆栈中删除了所有页面并导航到主页