J4N
J4N

Reputation: 20717

ERROR TypeError: Cannot read property 'loadChildren' of undefined

I just added some module + routing + AuthGuard to redirect to those (login)pages.

Everything seems to work(I can navigate to them, the links between the pages works, the AuthGuard properly redirects), but I get this error in the chrome logs:

core.js:4197 ERROR TypeError: Cannot read property 'loadChildren' of undefined
    at RouterPreloader.processRoutes (router.js:5220)
    at RouterPreloader.processRoutes (router.js:5222)
    at RouterPreloader.preload (router.js:5208)
    at MergeMapSubscriber.project (router.js:5203)
    at MergeMapSubscriber._tryNext (mergeMap.js:46)
    at MergeMapSubscriber._next (mergeMap.js:36)
    at MergeMapSubscriber.next (Subscriber.js:49)
    at FilterSubscriber._next (filter.js:33)
    at FilterSubscriber.next (Subscriber.js:49)
    at Subject.next (Subject.js:39)

I'm using angular 10.0.14, not sure what is causing this issue?

The issue happens only when I'm in one of those new page.

Here is my auth module:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';
import { AuthRoutingModule } from './auth-routing.module';
import { LostPasswordComponent } from './lost-password/lost-password.component';
import { SignupComponent } from './signup/signup.component';



@NgModule({
  declarations: [LoginComponent, SignupComponent, LostPasswordComponent, SignupComponent],
  imports: [
    CommonModule,
    AuthRoutingModule
  ]
})
export class AuthModule { }

Here is the AuthRoutingModule:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { LostPasswordComponent } from './lost-password/lost-password.component';
import { SignupComponent } from './signup/signup.component';

const routes: Routes = [
  {
    path: '',
    redirectTo: 'signin',
    pathMatch: 'full',
  },
  {
    path: 'signin',
    component: LoginComponent,
  },
  {
    path: 'signup',
    component: SignupComponent,
  },
  ,
  {
    path: 'lost-password',
    component: LostPasswordComponent,
  },
];

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

The pages are just empty pages with links between each other, like:

<p>login works!</p>
<a [routerLink]="['/auth/signup']" routerLinkActive="router-link-active" >Sign up instead</a>
<a [routerLink]="['/auth/lost-password']" routerLinkActive="router-link-active" >Password lost?</a>

If I go in one of the page of my other modules, no errors.

The AppRouting module:

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { AuthGuard } from './auth/auth.guard';
const routes: Routes = [
  {
    path: '',
    redirectTo: 'folder/Inbox',
    pathMatch: 'full',
  },
  {
    path: 'folder/:id',
    loadChildren: () => import('./folder/folder.module').then((m) => m.FolderPageModule),
    canActivate: [AuthGuard],
  },
  {
    path: 'auth',
    loadChildren: () => import('./auth/auth.module').then((m) => m.AuthModule),
  },
];

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

Any idea what is the issue? Or how to fix it?

Upvotes: 1

Views: 3499

Answers (3)

Luyang Du
Luyang Du

Reputation: 180

In our case, we just need to add the aot option (our project is a little aged):

{
...
        "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "aot": true,
...

We have some syntax newly added and the aot option helps during the build phase. Changing the syntax to a traditional way also solves it.

Upvotes: 0

Franklin Pious
Franklin Pious

Reputation: 3848

Actually the error is an extra comma in your AuthRoutingModule. Removing this should probably fix this error.

  {
    path: 'signup',
    component: SignupComponent,
  },
  ,

Upvotes: 1

Shahar Shokrani
Shahar Shokrani

Reputation: 8762

You can try to export the AuthRoutingModule:

@NgModule({
  declarations: [LoginComponent, SignupComponent, LostPasswordComponent, SignupComponent],
  imports: [
    CommonModule,
    AuthRoutingModule
  ],
  export: [AuthRoutingModule]
})
export class AuthModule { }

Upvotes: 0

Related Questions