0

i have just started to learn angular4, and i am a little curious to learn the concept of lazy loading in angular4. i have been going through this article and learned a little on lazy loading, but my question is in this article which i have mentioned above, there is only one component in the lazy module, what if i want more than one component in the lazy loading module, then how could i declare that in the route config of the lazy loading module. currently the route config for the lazy loading is

const routes: Routes = [ { path: '', component: LazyComponent } ];

what if i want another component, then how would be my route config?

5
  • 1
    I do not believe you are currently lazy-loading with that configuration. Lazy loading according to the documentation angular.io/guide/router#milestone-6-asynchronous-routing can be done with the loadChildren attribute. Commented Dec 27, 2017 at 18:45
  • plaese see my updated link Commented Dec 27, 2017 at 18:45
  • 1
    To answer your questions though, you wouldn't lazy load more than one component. Instead the lazy loaded component would have other components inside of it. <lazy-component> <nested-component></nested-component> </lazy-component> Commented Dec 27, 2017 at 18:47
  • @TommyMay cant there be anything like this in the lazy module route const routes: Routes = [ { path: '', component: LazyComponent } , { path: '/anotherComponent', component: AnotherLazyComponent } ]; Commented Dec 27, 2017 at 18:58
  • @LijinJohn Yes, that approach would also work, and you would be injecting your components into the <router-outlet> used in app-root in your example. Commented Dec 27, 2017 at 19:21

1 Answer 1

1

You can define routes using children in your lazy modules' routes:

const gtPermitRoutes: Routes = [
    { path: '', component: PermitComponent, children: [
        {path: '', component: PermitListComponent},
        {path: ':permitNumber', component: PermitDetailComponent},
     ]}
 ];

And any component you declare in your lazy module would be lazily loaded, including the components that don't appear in your routes, but used using selectors in your template:

@NgModule({
    declarations: [
        PermitComponent,
        PermitDetailComponent,
        AnotherComponent, // selector <app-another-component>
        PermitListComponent
    ],
    imports: [

    ]
})
export class PermitModule {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.