在不创建循环依赖的情况下从延迟加载的 routing.module 中检索路由到 child 组件

Retrieve routes from lazily loaded routing.module into child component without creating a circular dependency

场景

我想达到的目标

目前的情况

app-routing.module.ts(部分)

...
export const routes: Routes = [
...
  {
    path: 'admin',
    loadChildren: () => import('./pages/admin/admin.module').then(m => m.AdminModule),
    canActivate: [AuthGuard],
    data: {
      title: 'Administration',
      icon: 'grade',
      label: 'ADMIN',
      labelAddition: ''
    }
  },
...
]
...

admin-routing.module.ts(部分)

...
export const routes: Routes = [
...
  {
    path: '',
    component: AdminComponent
  },
...
]
...

admin.component.ts

import { Component } from '@angular/core';
import { routes } from './admin-routing.module';

@Component({
  template: `<app-sub-nav [links]="links"></app-sub-nav>`
})
export class AdminComponent {
  links = routes;
  constructor() { }
}

sub-nav.component.html(部分)

...
<a *ngFor="let link of links" [routerLink]="['./' + link.path]">
...

有什么问题?

Circular dependency detected:
src\app\pages\admin\admin-routing.module.ts -> src\app\pages\admin\admin.component.ts -> src\app\pages\admin\admin-routing.module.ts

到目前为止我尝试了什么?

admin-routing.module.ts(部分)

...
export const routes: Routes = [
...
  {
    path: '',
    component: AdminComponent,
    data: {
      routes: [
      {
        path: '...',
        data: {
         icon: '...'
...
        }
...
      },
...
      ]
    }
  },
...
]
...

有什么想法吗?

如果我正确理解了这种情况:您想使用来自延迟加载和受保护模块的链接来填充您的导航。所以你想访问延迟加载模块的路由来构建你的侧边导航。我的建议是在根范围内创建一个名为 NavigationService 的服务。制作负责跟踪导航链接的导航服务。提供方法 add remove 链接和事件(更可能是可观察的),例如 navigationChanged。在您的惰性模块中导入该服务并调用 add 方法来添加链接,并在您的侧面板组件中导入相同的服务并订阅 navigationChanged 事件以修改您的导航布局。