1

I am working on creating lazy loaded modules In spite of all my efforts, I think I am missing something here due to which I'm unable to load modules on demand.

I have my project structure as below:

screenshot of project structure

app
 -users
  -homeComponent
  -signupComponent 
  -users.routing.module.ts
  -users.module.ts
 -list
  -createListComponent
  -dashboardComponent 
  -list.routing.module.ts
  -list.module.ts

users-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { SignupComponent } from './signup/signup.component';

const routes: Routes = [
  {
    path: "",
    component: HomeComponent
  },
  {
    path: "/signup",
    component: SignupComponent
  }

];

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

app-routing.module.ts

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

const routes: Routes = [
  {
    path: 'signup',
    loadChildren: './app/users/users.module#UsersModule',
  },
];

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

I have added relative path in my loadChildren tag, but I am still getting an error saying "cannot load module". I have tried different websites but I feel I am missing a basic part here.

screenshot of error

Any help would be appreciated.

2
  • Here is working demo of lazyload module stackblitz.com/edit/santosh-angular-routing-concepts Commented Aug 31, 2020 at 3:56
  • @SantoshShinde Thank you for the working Demo, As per the project, i have modified my link to routing module. Still getting the same issue : { path: 'signup', loadChildren: 'app/users/users.module#UsersModule', }, I somehow landing in to same issue again Commented Aug 31, 2020 at 4:22

4 Answers 4

1
  1. Add users component in the users module.
  2. Add users component will be the container where other child component get loaded.
  3. Add <router-outlet></router-outlet> in the app componentcompoent

enter image description here

users-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { SignupComponent } from './signup/signup.component';
import { UsersComponent } from './users.component';

const routes: Routes = [
  {
    path: '',
    component: UsersComponent, // will be bootstrap component for users module
    children: [ // will children for user module
      {
        path: 'signup',
        component: SignupComponent,
      },
      {
        path: 'home',
        component: HomeComponent,
      },
    ]
  }
];

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

Here is working demo https://stackblitz.com/edit/angular-ivy-j6wtlk

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

Comments

1

Lazy Loading Syntax now uses promise / observable, Try this:

const routes: Routes = [
  {
    path: 'signup',
    loadChildren: () => import('./app/users/users.module').then(u => u.UsersModule)
  },
];

6 Comments

Should i change the syntax in my app.routing.module.ts file or users.routing.mmodule.ts file? When i changed the syntax in app.routing.module.ts file, it is throwing an error. Please clarify the changes
In app-routing.module.ts you should make that change.
I am getting below error with your change. src/app/users/users.module.ts:15:32 - error TS2307: Cannot find module './app/users/users.module' or its corresponding type declarations.
That means either the path is not correct or there is typo. Can you please create stackblitz demo?
Thanks for your help. below is the link of stackblitz , stackblitz.com/edit/angular-ivy-6ce1kw?file=src/app/users/…
|
0

I think there is a problem with your route configuration.

const routes: Routes = [
  {
    path: "",
    component: HomeComponent
  },
  {
    path: "/signup",
    component: SignupComponent
  }

];

With a configuration like this, the SignupComponent route object will never be reached, because Angular router will go through the configuration arrays until it finds the first match(in a DFS manner) and since every possible route matches "", it will search from there.

What I think you can do is to add the pathMatch: 'full' option:

const routes: Routes = [
  {
    path: "",
    component: HomeComponent,
    pathMatch: 'full',

  },
  {
    path: "/signup",
    component: SignupComponent
  }

];

What you could also do is to add the HomeComponent route add the end of the array as it is.

Comments

0

Working code in stackblitz

just make the changes as below, as lazy loaded module does not require path value again and thus can be left blank

// users-routing.module.ts
const routes: Routes = [
  {
    path: '', // leave blank
    component: SignupComponent,
  },
];
<!-- app.component.html -->
<p> home works</p>
<router-outlet></router-outlet>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.