简体   繁体   中英

Angular 8, Lazy loading both parent and child components as default route

I have the following routing code:

export const AppRoutes: Routes = [
  {
    path: '',
    redirectTo: 'landing',
    pathMatch: 'full'
  },
  {
    path: 'landing',
    component: AdminLayoutComponent,
    children: [
      {
        path: '',
        loadChildren: './pages/landing/landing.module#LandingModule'
      }
    ]
  },
  {
    path: '**',
    redirectTo: 'landing'
  }
];

The thing to note here is that the LandingModule is the child of the parent, which is the AdminLayoutComponent.

If I type in the full URL ( http://mypage.com/landing ), I get the "full page". In other words, the AdminLayoutComponent is rendered, as is the child, LandingModule.

HOWEVER, if I just enter the site's root URL ( http://mypage.com ), and allow the redirectTo to load the /landing page, then the only thing that renders is the LandingModule, NOT the parent AdminLayoutComponent.

Any thoughts on why this is happening, or how to fix it?

I don't know exactly what is going on in your example, but this is the approach I'm taking in my projects (applied to your scenario):

export const AppRoutes: Routes = [
  {
    path: '',
    component: AdminLayoutComponent,
    children: [
      { path: 'landing', loadChildren: './pages/landing/landing.module#LandingModule' },
      { path: '', redirectTo: "landing", pathMatch: "full" }
    ]
  },
  {
    path: '**',
    redirectTo: 'landing'
  }
];

Hope this helps.

export const AppRoutes: Routes = [
  {
    path: '',
    loadChildren: './pages/landing/landing.module#LandingModule'
  },

];

in landing-routing.module

  {
      path: "",
      component: landingComponent
    },

try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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