简体   繁体   English

懒加载辅助路由

[英]Lazy loading auxiliary routes

Here the scenario, I have a mainpage with 3 routes (child of mainpage each lazy loaded). 这里的场景,我有一个主页有3条路线(每个懒人加载的主页的子)。 在此输入图像描述

Here the auxiliary component has one primary outlet and other auxiliary as 这里辅助部件有一个主出口和其他辅助部件

<router-outlet name="auxone"></router-outlet>

and this is how my mainpage.route file looks 这就是我的mainpage.route文件的样子

在此输入图像描述

Now my auxilary route loads perfectly and im able to route in primary outlet with any problem. 现在我的辅助路线装载完美,并且能够在主要出口处路线出现任何问题。 But as soon as i try to load a route in the auxiliary outlet like this 但是一旦我尝试像这样在辅助插座中加载路线 在此输入图像描述

& try to navigate them using 并尝试使用它们进行导航

在此输入图像描述 (Aux route has one child as its own, and im trying to load another route as another child for the same aux) i get errors: (辅助路线有一个孩子作为自己的,我试图加载另一个路线作为同一辅助的另一个孩子)我得到错误:

Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'mainpage/aux'

As im unable to see any documentation on how to load and route aux routes lazy loading im at my wits end finding out how to make this work. 由于我无法看到任何关于如何加载和路由辅助路由延迟加载的文档,我的智慧最终找到如何使这项工作。

where exactly am i going wrong? 我到底哪里错了?

Here's my project structure 这是我的项目结构 在此输入图像描述

You should not and can not include the outlet: parameter on the lazy load path. 您不应该也不能在延迟加载路径中包含outlet:参数。 the lazy loaded routes are hidden away from the eagerly loaded application by design. 延迟加载的路径被设计隐藏在急切加载的应用程序之外。

Instead you should build a module for outlets components and it should have its own routing configuration. 相反,您应该为插座组件构建一个模块,它应该有自己的路由配置。 In that router configuration you can set up a route to your Auxiliary outlets. 在该路由器配置中,您可以设置到辅助插座的路由。

You did nothing wrong, it just was a long lasting bug in angular router, that got fixed (partly) in angular@6.1. 你没有做错任何事,它只是角度路由器中一个持久的错误,它在angular@6.1中得到了修复(部分)。 Partly because, to achieve your goal, you have to configurate the lazy loaded module in your app routes as unnamed route and instead name the parent route in your lazy loaded module router configuration: 部分原因是,为了实现您的目标,您必须将应用程序路由中的延迟加载模块配置为未命名路由,而是在延迟加载模块路由器配置中命名父路由:

app.routes app.routes

const routes: Routes = [
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full'
  },
  {
    /* no name for lazy loaded route */
    path: '',
    loadChildren: './home/home.module#HomeModule'
  },
  {
    /* you can add multiple empty routes without conflicts */
    path: '',
    loadChildren: './about/about.module#AboutModule'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}

home.routes home.routes

export const homeRoutes: Routes = [
  {
    /* your lazy loaded module route needs to be named */
    path: 'home',
    component: HomeComponent,
    children: [
      {
        path: '',
        component: HomeListComponent
      },
      {
        path: 'form',
        component: HomeFormComponent,
        outlet: 'popup'
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(homeRoutes)],
  exports: [RouterModule]
})
export class HomeRoutingModule {
}

while your home component template look like this: 而您的主组件模板如下所示:

<router-outlet></router-outlet>
<router-outlet name="popup"></router-outlet>

and the routerLink has to look like this 并且routerLink必须看起来像这样

[routerLink]="['/home', { outlets: { popup: ['form'] } }]"

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

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