导航到新路由时,要保留旧路由的url



我有一个要求,如果我在localhost:4200/home上,我们得到一个特定的错误,我导航到ErrorComponent路由而不改变url,即保持localhost:4200/home作为url。

我尝试使用NavigationExtras和指定的skipLocationChange为true,但这会清除当前路由,并且不会在URL中显示新路由,但会成功导航到错误页面。

有什么想法吗?

你可以尝试使用*ngIf指令加载组件,如下面的伪代码所示,

//routing.module
const routes: Routes = [
{path: "/home", component: HomeComponent}
];
//HomeComponent.html
<div *ngIf={{!error}}>
//code of home component goes here
</div>
<div *ngIf={{error}}>
<app-error></app-error>
</div>

//ErrorComponent.ts
selector: app-error

最新更新