admin管理员组

文章数量:1307570

How to define child routes of lazy loaded standalone component? In app.routes.ts I defined:

export const routes: Routes = [
    {
    path: 'some/parent',
    loadComponent: () => import('./my-parent/my-parentponent').then((m) => m.ParentComponent),
    children: [
      { path: '', redirectTo: 'child1', pathMatch: 'full' },
      { path: 'child1', loadComponent: () => import('./my-parent/child-one/child-oneponent').then(m => m.ChildOneComponent) },
      { path: 'child2', loadComponent: () => import('./my-parent/child-two/child-twoponent').then(m => m.ChildTwoComponent) },
    ]
  },...

The parent component template contains a router-outlet:

<head></head>
<router-outlet></router-outlet>
<foot></foot>

Does this approach align with Angular's standalone architecture? Is it possible, or does it make sense, for only the parent component to be lazy-loaded while the child components are not?

I'm using an Angular 19 SSR standalone app.

How to define child routes of lazy loaded standalone component? In app.routes.ts I defined:

export const routes: Routes = [
    {
    path: 'some/parent',
    loadComponent: () => import('./my-parent/my-parentponent').then((m) => m.ParentComponent),
    children: [
      { path: '', redirectTo: 'child1', pathMatch: 'full' },
      { path: 'child1', loadComponent: () => import('./my-parent/child-one/child-oneponent').then(m => m.ChildOneComponent) },
      { path: 'child2', loadComponent: () => import('./my-parent/child-two/child-twoponent').then(m => m.ChildTwoComponent) },
    ]
  },...

The parent component template contains a router-outlet:

<head></head>
<router-outlet></router-outlet>
<foot></foot>

Does this approach align with Angular's standalone architecture? Is it possible, or does it make sense, for only the parent component to be lazy-loaded while the child components are not?

I'm using an Angular 19 SSR standalone app.

Share Improve this question asked Feb 2 at 15:07 MurmulodiMurmulodi 6891 gold badge5 silver badges23 bronze badges 1
  • 1 It all depends on what you want to achieve. Lazy-loading child components can be beneficial for large applications, especially if they are independent or infreq used. But if these adjectives do not describe your child component it will prob just add overhead cost for the client. Be mindful that the lazy loaded components will be visible as a downloaded package in your network tab. But if you inspect your main page, you will see only what is rendered by the server in html. If your users are going to have to go through this part of the app often, you might want to have it rendered by the server. – H3AR7B3A7 Commented Feb 2 at 16:01
Add a comment  | 

1 Answer 1

Reset to default 4

it feels like subroutes of the ParentComponent should be that "module's" responsibility, and not bloat the root routing configuration

to achieve that simply use loadChildren

// main routes
export const routes: Routes = [
  {
    path: 'some/parent',
    loadChildren: () => import('./my-parent/my-parentponent'),
  },...
// parentponent

export class ParentComponent {...}

const routes: Route[] = [
  {
    path: '',
    component: ParentComponent,
    children: [
      { path: '', redirectTo: 'child1', pathMatch: 'full' },
      { path: 'child1', loadComponent: () => import('./child1/child1ponent') /* don't need then piece, if component is "export default" */ },
      { path: 'child2', component: ChildTwoComponent },
    ]
  }
]

export default routes;

本文标签: Angular 19 routing child components in standalone architectureStack Overflow