Angular 2 lazily loaded routes that contain aux router outlet

╄→гoц情女王★ 提交于 2019-11-29 12:42:42

Just an update to provide a solution to this problem. After reading through the issue reported on github, Keith Gillette's solutions works after I fiddled around with it a bit.

https://github.com/angular/angular/issues/10981#issuecomment-301787482


Solution:

The way that I constructed my routes are as follows:

  1. Initial empty path with no component declared.
  2. You will then declare child routes on this empty path with all your sub paths.
  3. The sub paths declare the path and the base component that contains your aux router outlet. In my case the SettingsComponent contains my settings router outlet.
  4. Each sub path will then declare another set of children containing an empty path with the component to be loaded in the aux router outlet.

Below is an example of my routes that are working.

app.routing.ts (stays the same)

const appRoutes: Routes = [
  {
    path: 'schedule',
    loadChildren: '@app/schedule/schedule.module#ScheduleModule'
  }
];

settings.routing.ts

export const SETTINGS_ROUTES = [
  {
    path: '',
    children: [
      {
        path: 'schedule',
        component: SettingsComponent,
        children: [
          {
            path: '',
            component: SettingsScheduleComponent,
            outlet: 'settings'
          }
        ]
      },
      {
        path: 'user',
        component: SettingsComponent,
        children: [
          {
            path: '',
            component: SettingsUserComponent,
            outlet: 'settings'
          }
        ]
      }
    ]
  }
];

Note:

One thing to note is that the paths no longer follow the aux router path pattern.

Aux Route Path

<a [routerLink]="['/settings', { outlets: { settings: 'user'} }]">
  User
</a>

New Routing Paths

<a [routerLink]="['/settings/user']">
  User
</a>

Conclusion

This solutions works for my routing. I did not test it for multiple nested child routes though. Reading some of the comments on the above linked github issue, some are reporting that child routes where you declare the component to be loaded in the aux router outlet only seem to work with empty paths. I did not test this. You may need to continue nesting empty router paths with aux routers to workaround this. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!