首页 > 解决方案 > router.navigate 中的 {skipLocationChange : true} 不起作用;改变状态

问题描述

我正在尝试在 JSP 页面上嵌入 Angular 应用程序,出于某些原因,我需要浏览器来保留状态并且不希望 Angular 将新状态推送到浏览器历史记录。

根据Angular 文档 { skipLocationChange: true },我可以这样做。下面是我修改后的代码。

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

Angular 仍然在浏览器历史中推动新的历史状态。

state: {navigationId: 2}

下面是控制台快照。

在此处输入图像描述

路由代码有什么问题吗?或者我缺少任何参数。

标签: angularangular-routingwindow-object

解决方案


问题是 app.module.ts 中的路由配置。由于 Angular 将默认路径路由到customComponent,它会修改状态并且没有提供传递skipLocationChange参数的规定,我不得不使用skipLocationChange从 appComponent 构造函数重新路由到 customComponent ,这在我的例子中是自举的。

根据编写的代码,直接引导 customComponent 也将有所帮助。

const appRoutes: Routes = [
{
    path: '',
    redirectTo: "/customComponent",
    pathMatch: 'full'
},

推荐阅读