簡體   English   中英

在 Angular 8 路由父路由與其他父路由導航時保持不變

[英]In Angular 8 Router parent route remains the same when navigating to the route with other parent

我正在開發一個在 Angular (v8.2.8) 上編寫的項目,並使用相同版本的路由器。 路由的架構如下:

我有 app-routing.module.ts,其中有 3 個routes數組條目: Shell.childRoutes([...array of routes]), Case.childRoutes([...array of routes]), { path: '**', redirectTo: '/', pathMatch: 'full' }

Shell - 這是應用程序內部頁面的父組件。 案例 - 這是應用程序公共頁面的父組件。

方法子路由對兩個組件的作用幾乎相同:

export class Shell {
  static childRoutes(routes: Routes): Route {
    return {
      path: 'app',
      component: ShellComponent,
      children: routes,
      canActivate: [AuthenticationGuard],
      data: { reuse: true }
    };
  }
}

export class Case {
  static childRoutes(routes: Routes): Route {
    return {
      path: '',
      component: CaseComponent,
      children: routes,
      data: { reuse: true }
    };
  }
}

應用程序具有Shell父級的dashboard組件和Case父級的home組件。 用戶在home路徑加載頁面,標記如下:

<app-root>
  <app-case> 
    <router-outlet></router-outlet>
    <app-home></app-home>
  </app-case>
</app-root>

在重定向到dashboard路線后,我希望有以下標記:

<app-root>
  <app-shell> 
    <router-outlet></router-outlet>
    <app-dashboard></app-dashboard>
  </app-shell>
</app-root>

但我收到

<app-root>
  <app-case> 
    <router-outlet></router-outlet>
    <app-dashboard></app-dashboard>
  </app-case>
</app-root>

所以確切的問題在於測試用例:用戶在Shell父級中存在的路由上。 然后,如果用戶將被重定向到Case父級中的任何路由 - 父級將保留 Shell。 同樣的情況也適用於其他方向。 如果先加載Case路由,則在重定向到Shell路由父級后將保持這種情況。

我嘗試在childRoutes方法中編輯不同的參數,但沒有成功,我只是不明白是引擎中的問題,還是我只需要指定一些額外的參數。

基本上我解決了這個問題。 對於那些有同樣問題的人 - 對我來說,問題在於實施的自定義路線可重用策略。 當我從Case切換到Shell route-reusable-strategy function shouldReuseRoute返回 true,所以路由被重用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM