简体   繁体   English

Angular 6 - 路由到懒惰模块的子模块不起作用

[英]Angular 6 - Routing to child module of a lazy module not working

I'm trying to develop an application where the root module will display a list of router links that will load lazy modules:我正在尝试开发一个应用程序,其中根模块将显示将加载惰性模块的路由器链接列表:

const appRoutes: Routes = [
{
 path: 'compose',
 component: ComposeMessageComponent,
 outlet: 'popup'
},
{
 path: 'admin',
 loadChildren: 'app/admin/admin.module#AdminModule',
 canLoad: [AuthGuard]
},
{
 path: 'crisis-center',
 loadChildren: 'app/crisis-center/crisis-center.module#CrisisCenterModule',
 data: { preload: true }
}
];

In this particular case the admin module will have two router links for components declared by itself and one router link for another module (manage-heroes):在这种特殊情况下,admin 模块将有两个用于自己声明的组件的路由器链接和一个用于另一个模块(管理英雄)的路由器链接:

const adminRoutes: Routes = [
  {
    path: '',
    component: AdminComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: '',
        canActivateChild: [AuthGuard],
        children: [
          { path: 'crises', component: ManageCrisesComponent },
          {path: 'heroes', component: ManageHeroesComponent},
          { path: '', component: AdminDashboardComponent }
        ]
      }
    ]
  }
];

I can navigate fine to all paths, but if go to heroes and try to navigate to one of its own routes (eg zeroes) I get a page not found.我可以很好地导航到所有路径,但是如果转到英雄并尝试导航到它自己的路线之一(例如零),我会发现找不到页面。 The heroes routing looks like this:英雄路由看起来像这样:

const manageHeroesRoutes: Routes = [
  {
    path: '',
    component: ManageHeroesComponent,
    children: [
        { path: 'zeroes', component: ManageZerosComponent },
        { path: 'friends', component: ManageFriendsComponent }
    ]
  }
];

I notice that if I replace the heroes path in admin routes to:我注意到,如果我将管理路由中的英雄路径替换为:

children: [
  { path: 'crises', component: ManageCrisesComponent },
  {path: 'heroes', loadChildren: 'app/admin/manage-heroes/manage-heroes.module#ManageHeroesModule'},
  { path: '', component: AdminDashboardComponent }
]

I can access the heroes routes just fine.我可以很好地访问英雄路线。 But I would like to understand why can't I access them without lazy loading the ManageHeroes module.但是我想了解为什么我不能在不延迟加载 ManageHeroes 模块的情况下访问它们。

Here is a link to stackblitz with a reproduction of my problem https://stackblitz.com/edit/angular-pm9wsz这是stackblitz的链接,其中重现了我的问题https://stackblitz.com/edit/angular-pm9wsz

Thanks.谢谢。

When you use component in the route path, you define a component to handle this route.当你在路由路径中使用组件时,你定义了一个组件来处理这个路由。 The Angular will load only that component. Angular 将仅加载该组件。 In you case, the component is in other module, but the angular will not load none of the other module itens, including its routes.在您的情况下,组件在其他模块中,但 angular 不会加载任何其他模块,包括其路由。

When use a "load children" at route path definition, the angular will lazy load the module and your itens, including its inner routes.在路由路径定义中使用“加载子项”时,角度将延迟加载模块和您的项目,包括其内部路由。

https://angular.io/guide/lazy-loading-ngmodules https://angular.io/guide/lazy-loading-ngmodules

Your mistake is what you're trying to see crises and heroes as a child routes of AdminComponent but they are on the same level as AdminComponent .你的错误是你想看到什么crisesheroes作为一个孩子的路线AdminComponent ,但他们都在同一水平AdminComponent Try to correct your code:尝试更正您的代码:

const adminRoutes: Routes = [
    {
        path: '',
        component: AdminComponent,
        canActivate: [AuthGuard],
    },
    {path: 'crises', component: ManageCrisesComponent},
    {path: 'heroes', component: ManageHeroesComponent}
];

Now angular will match your routes regarding the base / as现在 angular 将匹配您关于 base / as 的路线

  • admin to AdminComponent admin到 AdminComponent
  • admin/crises to ManageCrisesComponent admin/crises到 ManageCrisesComponent
  • admin/heroes to ManageHeroesComponent admin/heroes到 ManageHeroesComponent

Fork from angular official example with added child route: https://stackblitz.com/edit/angular-omprkj来自 angular 官方示例的 fork 并添加了子路由: https : //stackblitz.com/edit/angular-omprkj

in ManageHeroesComponent.html use <router-outlet></router-outlet> to call child component在 ManageHeroesComponent.html 中使用<router-outlet></router-outlet>来调用子组件

  {
    path: '',
    children: [
        { path: "" , component: ManageHeroesComponent },
        { path: 'zeroes', component: ManageZerosComponent },
        { path: 'friends', component: ManageFriendsComponent }
    ]
  }
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM