错误TypeError:无法读取未定义的属性'loadChildren'

时间:2020-09-08 20:12:49

标签: angular angular-ui-router angular-routing

我刚刚添加了一些模块+路由+ AuthGuard来重定向到那些(登录)页面。

一切似乎都可以正常工作(我可以导航到它们,页面之间的链接可以正常工作,AuthGuard可以正确重定向),但是在Chrome日志中却出现此错误:

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)

我正在使用角度10.0.14,不确定是什么原因导致了此问题?

仅当我进入这些新页面之一时,问题才会发生。

这是我的身份验证模块:

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 { }

这是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 {}

页面只是空白页面,彼此之间具有链接,例如:

<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>

如果我进入其他模块的页面之一,则没有错误。

AppRouting模块:

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 {}

任何主意是什么问题?或如何解决?

2 个答案:

答案 0 :(得分:0)

您可以尝试导出AuthRoutingModule

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

答案 1 :(得分:0)

实际上,错误是您的 AuthRoutingModule 中多了一个逗号。删除它应该可以解决这个错误。

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