简体   繁体   English

Angular 9 - Lazy Laded Module 的子路由不起作用(无法匹配任何路由)

[英]Angular 9 - Child Routes of Lazy Laoded Module are not working (cannot match any routes)

im trying to implement lazy loading for my backend of the app.我正在尝试为我的应用程序后端实现延迟加载。 Now it tells me there are no such routes.现在它告诉我没有这样的路线。

My AppRoutingModule我的 AppRoutingModule

const routes: Routes = [
  {
    path: '',
    component: StartPageComponent,
    pathMatch: 'full',
  },
  {
    path: 'schulungen/:name',
    component: StartPageComponent,
  },
  {
    // ToDo: AuthGuard
    path: 'hdv',
    loadChildren: () => import('./pages/hdv-page/hdv-page.module').then(m => m.HdvPageModule),
  },
];

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

My HdvRoutingModule我的 HdvRoutingModule

const hdvRoutes: Routes = [
  {
    path: '', component: HdvPageComponent, pathMatch: 'full', children: [
      {path: '', component: DashboardComponent, pathMatch: 'full'},
      {path: 'bookings', component: RegistrationsComponent},
      {path: 'bookings/:id', component: SingleRegistrationComponent},
      {path: 'dimensions', component: EventsComponent},
      {path: 'dimensions/create', component: CreateEventComponent},
      {path: 'dimensions/edit/:id', component: EditEventComponent},
      {path: 'dimensions/:id', component: SingleEventComponent},
      {path: 'settings', component: SettingsComponent},
    ]
  },
];

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

When I go to the route /hdv The HdvPageComponent loads correctly with the Dashboardcomponent in the of HdvPageComponent like I would it expect to do.当我转到路线 /hdv 时, HdvPageComponentHdvPageComponentDashboardcomponent一起正确加载,就像我期望的那样。 But when I go to /hdv/bookings I get the error Cannot match any routes. URL Segment: 'hdv/bookings'但是当我去/hdv/bookings我收到错误Cannot match any routes. URL Segment: 'hdv/bookings' Cannot match any routes. URL Segment: 'hdv/bookings'

Any idea?任何的想法?

You are having conflicts using the pathMatch in your HdvRoutingModule .您正在使用pathMatch在有冲突HdvRoutingModule You only need to remove that property.您只需要删除该属性。

I write some code and you can see the solution here .我写了一些代码,你可以在这里看到解决方案。

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
import { HdvPageComponent } from './hdv-page/hdv-page.component';
import { RegistrationsComponent } from './registrations/registrations.component';

const hdvRoutes: Routes = [
  {
  path: '', 
  component: HdvPageComponent,
  children: [
      {
        path: '',
        component: DashboardComponent
      },
      {
        path: 'bookings',
        component: RegistrationsComponent
      },
    ]
  },
];

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

hdv-page.component.html

<router-outlet></router-outlet> <!-- outer outlet in hdv-page component to see its children -->

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

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