【问题标题】:Angular 12 - Navigate to a new route keeping the url of the old routeAngular 12 - 导航到保留旧路线网址的新路线
【发布时间】:2021-07-29 06:26:19
【问题描述】:

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

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

有什么想法吗?

【问题讨论】:

    标签: angular


    【解决方案1】:

    您可以尝试使用 *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
    

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多