Angular 6 - Children paths of lazy loaded module are not loading

扶醉桌前 提交于 2019-12-11 17:45:04

问题


I am trying to work with routing in lazy loaded modules, but it is not working.

This is my app module routing.

  export const routes: Routes = [
  {
    path: '',
    component: DefaultLayoutComponent,
    data: {
      title: 'Home'
    },
    children: [
      {
        path: 'holiday',
        loadChildren: './holiday/holiday.module#HolidayModule'
      }
    ]
  },
  {
    path:"**",
    component:P404Component
  }
];

This is RoutingModule for lazy-loaded module.

   const routes: Routes = [
  {
    path: '', children: [
      { path: '', component: HolidayBookingComponent },
      { path: ':id', component: HolidayBookingComponent },
      { path: 'booking', component: HolidayBookingComponent },
      { path: 'review', component: HolidayReviewComponent }
    ]
  },

];

I can navigate to http://localhost:4200 and http://localhost:4200/holiday correctly.

But when I try http://localhost:4200/holiday/1 it throws 404 in console.

GET http://localhost:4200/holiday/runtime.js net::ERR_ABORTED 404 (Not Found)

These are dependencies in package.json in case you need it.

"@angular/cli": "^6.2.6",
"@angular/router": "^6.1.10",
"@angular/core": "^6.1.10",

回答1:


Please look at this example

   https://angular-svs3xe.stackblitz.io

   https://stackblitz.com/edit/angular-svs3xe



回答2:


You probably want this for your lazy routes:

const routes: Routes = [
      { path: '', component: HolidayBookingComponent },
      { path: ':id', component: HolidayBookingComponent },
      { path: 'booking', component: HolidayBookingComponent },
      { path: 'review', component: HolidayReviewComponent }
];


来源:https://stackoverflow.com/questions/53389882/angular-6-children-paths-of-lazy-loaded-module-are-not-loading

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