3
votes

I have implemented a lazy loading in my Angular application. From the app.routes file I load the Login and Dashboard modules, until this point everything works fine with lazy loading. I have the problem when trying to load other daughter routes into dashboard.routes using loadChildrens, that is, children within the children.

app.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

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


const routes: Routes = [
  {
    path: 'login',
    loadChildren: 'app/modules/login/login.module#LoginModule'
  },
  {
    path: 'dashboard',
    loadChildren: 'app/modules/dashboard/dashboard.module#DashboardModule'
  },
  { path: '**', redirectTo: 'login', pathMatch: 'full' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

dashboard.module.ts

import { NgModule } from "@angular/core";
import { MainRoutingModule } from "@app/modules/main/main-routing.module";
import { MainLayoutComponent } from "@app/modules/main/views/main-layout/main-layout.component";
import { BlankComponent } from '../blank/blank.component';

@NgModule({
  imports: [DashboardRoutingModule],
  declarations: [DashboardComponent]
})
export class DashboardModule { }

dashboard-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashboardComponent } from '../blank/blank.component';

const routes: Routes = [
  {
    path: '',
    component: DashboardComponent,
    children: [
      {
        path: 'clients',
        loadChildren: './../clients/clients.module#CLientsModule'
      },
      {
        path: 'users',
        loadChildren: './../users/users.module#UsersModule'
      },
      { path: '', redirectTo: '/users', pathMatch: 'full' }
    ]
  }
];
@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class DashboardRoutingModule { }

clients.module

import { NgModule } from "@angular/core";

import { CommonModule } from '@angular/common';
import { ClientsComponent } from './clients.component';
import { CLientsRoutingModule } from './clients-routing.module';


@NgModule({
  imports: [
    CommonModule,
    CLientsRoutingModule
  ],
  declarations: [ClientsComponent]
})
export class CLientsModule { }

clients.routing

import { Routes, RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { CLientsComponent } from './clients.component';

const routes: Routes = [
    {
      path: '',
      component: CLientsComponent
    }
  ];
  @NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
  })
  export class ClientsRoutingModule { }

enter image description here

here was a general error. Uncaught (in promise): TypeError: Cannot read property 'split' of undefined TypeError: Cannot read property 'split' of undefined at defaultUrlMatcher (http://localhost:4200/vendor.js:83027:28) at match (http://localhost:4200/vendor.js:85220:15) at ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.matchSegmentAgainstRoute (http://localhost:4200/vendor.js:85066:18) at....

2

2 Answers

0
votes

I also had this error when I did not specify the route path.

-2
votes

You are trying to call a split method on the variable that isn't defined.

It is a bit hard to tell you exactly what is wrong without looking at your code, but make sure the variable is defined before you call the split method.

You can also add an if condition around the call to test if there's a value stored in your variable before calling the split if you can't guarantee that you will have a value.