Search code examples
javascriptangularangular4-routerangular-module

Routing module stop working: Uncaught (in promise): Error: Cannot find 'xxxModule'


I have a weird issue with routing module. The routing module used to work. However, after I added another project routing module in a different folder with different module name, I got error on this project when trying to navigate to step 1.

Error: Uncaught (in promise): Error: Cannot find 'Step1Module' in './+step1/step1.module'.

step1.module:

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

import { AbcComponent } from './abc.component';
import { StepGuardService } from './shared/step-guard/step-guard.service';

const routes: Routes = [
  {
    path: '', component: AbcComponent, children: [
      {
        path: '',
        redirectTo: 'step1',
        pathMatch: 'full'
      },
      {
        path: 'step1',
        loadChildren: './+step1/step1.module#Step1Module',
        data: {
          preload: true,
          stepNumberOnNavigator: 1
        },
        canActivate: [StepGuardService]
      },
      {
        path: 'step2',
        loadChildren: './+step2/step2.module#Step2Module',
        data: {
          preload: true,
          stepNumberOnNavigator: 2
        },
        canActivate: [StepGuardService]
      }
    ]
  }
];

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

To fix this, I have to re-type the module name "Step1Module" with the exactly same letters (basically copy and paste) on the line of

loadChildren: './+step1/step1.module#Step1Module',

and save the file. Then the error will be gone and the project will be running. I don't understand what's going on here. Since nothing actually changed, I can't push anything to git to fix the issue on the server side too.

Any help will be appreciated!

Thanks!

Updates: routing module for step1 and step2

step1-routing.module.ts:

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

import { Step1Component } from './step1.component';

const routes: Routes = [
  {
    path: '',
    component: Step1Component,
    data: {
      bannerTitle: 'xxx',
      pageTitle: 'xxx'
    }
  }
];

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

step2-routing.module.ts:

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

import { Step2Component } from './step2.component';
import { Step2aComponent } from '../+step2/step2a/step2a.component';
import { QuestionnaireDetailComponent } from './questionnaire-detail/questionnaire-detail.component';
import { EligibilityDeclineComponent } from './eligibility-decline/eligibility-decline.component';

const routes: Routes = [
  {
    path: '',
    component: Step2aComponent,
    data: {
      bannerTitle: 'xxx',
      pageTitle: 'xxx'
    }
  },
  {
    path: 'eligibility',
    component: Step2Component,
    data: {
      bannerTitle: 'xxx',
      pageTitle: 'xxx'
    }
  },
...
  }
];

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

Solution

  • Kindly apply full path to your module. Change this line to following in step1.module file:

     children: [
        {
          path: 'dashboard',
          loadChildren: 'app/components/dashboard/dashboard.module#DashboardModule'
        }
       ]
    

    these is only sample data please replace your data. let try this once and please let me know.

    Note :- The path start app/path/to/module/ like this full path.

    For your doubt visit these website and note that loadChildren line

    https://scotch.io/tutorials/lazy-loading-in-angular-v2

    https://angular.io/guide/lazy-loading-ngmodules

    I hope this solve your problem.

    Thanks,

    Karnan Muthu.