2

I am trying to lazy load the module with angular 2.

I have two modules

  1. AppModule (main module)
  2. ProductModule (lazily loaded module)

AppModule.ts

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        RouterModule.forRoot([
            { path: 'welcome', component: WelcomeComponent },
            { path: '', redirectTo: 'welcome', pathMatch: 'full' },

            { path: 'products', loadChildren:'app/products/product.module#ProductModule'}
        ])
    ],
    declarations: [AppComponent,
        WelcomeComponent
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

Here I have two routes one is default route that is blank and one is welcome screen in AppModule. It contains one more route products to initiate lazy loading of modules and relevant component that is ProductModule.

and now ProductModule looks like this:

@NgModule({
    declarations: [
        ProductListComponent,
        ProductDetailComponent,
        ProductFilterPipe
    ],
    imports: [
        SharedModule,
        RouterModule.forChild([
            { path: '', component: ProductListComponent },
            { path: 'product/:id', component: ProductDetailComponent, pathMatch:'full'}
        ])
    ],
    providers: [
        ProductService,
        ProductDetailGuard
    ]
})
export class ProductModule {

}

as you can see my ProductModule is having empty routes (for listing product) and one route with product/id which shows product detail.

Now for the first route in ProductModule having ProductListComponent is activated when I navigated to http://localhost:3000/products which is OK but for the second route which is having ProductDetailComponent is activated when I navigated to http://localhost:3000/products/product/1.

Now for the detail screen url I do not wanted to have products in the url.

I have already tried specifying { path: 'products', component: ProductListComponent } route. But unluckily it doesn't worked.

Please explain me why it needs the products in the detail screen URL and how can I make it activated on http://localhost:3000/product/1 URL?

1 Answer 1

4

I recognize this code. :-) Have you seen my routing course? I cover this. For lazy loaded routes the base route has to be the same. So both routes need to start with 'products'.

Here is an example:

    RouterModule.forChild([
      {
        path: '',
        component: ProductListComponent
      },
      {
        path: ':id',
        component: ProductDetailComponent
      },
      {
        path: ':id/edit',
        component: ProductEditComponent,
        canDeactivate: [ProductEditGuard]
      }
    ])

The matching routing name is required because all of the lazy loaded routes need to share a parent route. Accessing that parent route for any child will lazy load the routes.

Sign up to request clarification or add additional context in comments.

3 Comments

@DeborahK Isn't there any way out there so that I can make it http://localhost:3000/product/1 without changing route products? Also please provide the link to your course.
To answer your question ... you can remove lazy loading. Then you can name your routes anything you want. Or you could make your routes product and product/1. They just need to have the same parent route for lazy loading to work.

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.