Angular 4 Lazy Loading and Routes not working
Asked Answered
G

2

5

I have a module with the routes of my app. One of this routes is a lazy loading module.

The problem is that this lazy loading module haves inside a routes for child components. But on my router config this routes don't appears... So when i call the lazy module don't show anything on my screen.

Here is my router config (main module):

export const MODULE_ROUTES: Route[] =[
    { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
    { path: 'dashboard', component: HomeComponent, canActivate: [AuthGuard] },
    { path: 'calendar', loadChildren: 'app/dashboard/calendar/calendar-module.module#CalendarModuleModule',canActivate: [AuthGuard]}, 
    { path: '**', component: NoPageFoundComponent, pathMatch: 'full' }
]
.
.
.


@NgModule({
    imports: [
        RouterModule.forChild(MODULE_ROUTES)
.
.
.

On my lazy module:

export const MODULE_CALENDAR_ROUTES: Route[] = [
    {
        path: 'calendar', component: CalendarComponent, canActivateChild: [AuthGuard, CalendarGuard],
        children: [
            {
                path: '', component: MainCalendarComponent, canActivateChild: [AuthGuard, CalendarGuard]
            },
            {

                path: 'user', component: EditEventComponent, canActivateChild: [AuthGuard, CalendarGuard]
            }
        ]
    }
]

.
.
.

@NgModule({
    imports: [
        SharedModule,
.
.
.
        RouterModule.forChild(MODULE_CALENDAR_ROUTES)

If i print my router config this routes declaren on my lazy module don't show:

Routes:  [
  {
    "path": "dashboard",
    "canActivate": [
      null
    ]
  },
  {
    "path": "calendar",
    "loadChildren": "app/dashboard/calendar/calendar-module.module#CalendarModuleModule",
    "canActivate": [
      null
    ]
  },
  {
    "path": "**",
    "pathMatch": "full"
  },
  {
    "path": "dashboard"
  }
]

Can you help me?

Glop answered 4/6, 2017 at 16:4 Comment(1)
have you seen any error in console saying not found module or something..Biform
G
9

The problem was with the way I've declared my route on my lazy module:

export const MODULE_CALENDAR_ROUTES: Route[] = [
    {
        path: 'calendar',
        component: CalendarComponent,
        canActivateChild: [AuthGuard, CalendarGuard],
        children: [
            {
                path: '',
                component: MainCalendarComponent,
                canActivateChild: [AuthGuard, CalendarGuard]
            },
            {

                path: 'user',
                component: EditEventComponent,
                canActivateChild: [AuthGuard, CalendarGuard]
            }
        ]
    }
]

The path of CalendarComponent had to change from:

path: 'calendar', // wrong
component: CalendarComponent,
...

to the below:

path: '', // right
component: CalendarComponent,
...

Thanks to @jotatoledo on gitter that help me to solve this.

Glop answered 4/6, 2017 at 17:44 Comment(0)
B
0

in the main module of routing you have used forChild to load the paths, there isn't any root router to load childrens.

 @NgModule({
    imports: [
    RouterModule.forChild(MODULE_ROUTES)
  ]})

rather then that you should use

@NgModule({
    imports: [
    RouterModule.forRoot(MODULE_ROUTES)
 ]})

Another important thing should be remember about the lazy loading that you should use ** ModuleWithProviders** of core module

EX

Lazymodule route

import { NgModule, ModuleWithProviders } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [{
    path: '',
    component: ConsumerComponent,
    children: [{
        path: '',
        component: DashboardComponent
}]

export const ConsumerRoutingModule: ModuleWithProviders = RouterModule.forChild(routes);

main_module

@NgModule({
    imports: ['ConsumerRoutingModule']
})

there is an interesting blog about the lazy load : lazyload tutorial

Biform answered 14/6, 2017 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.