TypeError:在Angular中导入两个模块时,{undefined}不是函数

时间:2018-10-26 06:21:17

标签: angular angular6

我正在使用Angular 6

我在LayoutModule下有两个布局模块(AdminLayout和AuthLayout)

AdminLayoutModule 的内容是

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(AdminLayoutRoutes),
    FormsModule,
    DashboardModule
  ],
  declarations: [
    AdminLayoutComponent
  ]
})
export class AdminLayoutModule { }

admin-layout.routing.ts

export const AdminLayoutRoutes: Routes = [
  { path: 'dashboard', loadChildren: '../../dashboard/dashboard.module#DashboardModule' }
];

admin-layout.component.html 具有

<h2>admin-layout works!</h2>

<router-outlet></router-outlet>

类似地, AuthLayoutModule

的内容
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(AuthLayoutRoutes),
    FormsModule,
    AuthModule
  ],
  declarations: [
    AuthLayoutComponent
  ],
  exports: [
    RouterModule
  ]
})
export class AuthLayoutModule { }

auth-layout.routing.ts

export const AuthLayoutRoutes: Routes = [
  { path: '', redirectTo: 'login', pathMatch: 'full'},
  { path: 'login', component: LoginComponent},
  { path: 'logout', component: LogoutComponent}
];

auth-layout.component.html

<h2>auth-layout works!</h2>

<router-outlet></router-outlet>

以及 AdminLayoutModule AuthLayoutModule 都导入了 LayoutModule

@NgModule({
  imports: [
    CommonModule,
    AdminLayoutModule,
    AuthLayoutModule
  ],
  declarations: []
})
export class LayoutsModule { }

哪些进一步导入了 AppModule

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,

    LayoutsModule,

    HttpClientModule,
    RouterModule,
    PagesModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

但是当我运行服务器时,它在编译过程中没有给出任何错误,但是在浏览器中访问URL时却给出了错误。

core.js:1673 ERROR Error: Uncaught (in promise): TypeError: undefined is not a function
TypeError: undefined is not a function
    at Array.map (<anonymous>)
    at webpackAsyncContext ($_lazy_route_resource lazy namespace object:21)
    at SystemJsNgModuleLoader.push../node_modules/@angular/core/fesm5/core.js.SystemJsNgModuleLoader.loadAndCompile (core.js:4982)
    at SystemJsNgModuleLoader.push../node_modules/@angular/core/fesm5/core.js.SystemJsNgModuleLoader.load (core.js:4974)
    at RouterConfigLoader.push../node_modules/@angular/router/fesm5/router.js.RouterConfigLoader.loadModuleFactory (router.js:3261)
    at RouterConfigLoader.push../node_modules/@angular/router/fesm5/router.js.RouterConfigLoader.load (router.js:3249)
    at MergeMapSubscriber.project (router.js:1519)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:61)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:51)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
    at Array.map (<anonymous>)
    at webpackAsyncContext ($_lazy_route_resource lazy namespace object:21)
    at SystemJsNgModuleLoader.push../node_modules/@angular/core/fesm5/core.js.SystemJsNgModuleLoader.loadAndCompile (core.js:4982)
    at SystemJsNgModuleLoader.push../node_modules/@angular/core/fesm5/core.js.SystemJsNgModuleLoader.load (core.js:4974)
    at RouterConfigLoader.push../node_modules/@angular/router/fesm5/router.js.RouterConfigLoader.loadModuleFactory (router.js:3261)
    at RouterConfigLoader.push../node_modules/@angular/router/fesm5/router.js.RouterConfigLoader.load (router.js:3249)
    at MergeMapSubscriber.project (router.js:1519)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:61)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:51)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
    at resolvePromise (zone.js:814)
    at resolvePromise (zone.js:771)
    at zone.js:873
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:3811)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)

当我评论在 LayoutModule 中导入的任何一种布局时,另一种效果很好。但是,将两者一起导入会产生上述错误。

路由是在 app-routing.module.ts 中定义的,例如

const routes: Routes = [
  { path: 'auth', loadChildren: './layouts/auth-layout/auth-layout.module#AuthLayoutModule' },
  { path: 'admin', loadChildren: './layouts/admin-layout/admin-layout.module#AdminLayoutModule'},
  { path: '', redirectTo: 'admin/dashboard', pathMatch: 'full'},
  { path: '**', component: PageNotFoundComponent},
];

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

1 个答案:

答案 0 :(得分:1)

您不应将延迟加载的模块导入您的 app-module

  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    LayoutsModule,  //Remove this if this is your lazy load module
    HttpClientModule,
    RouterModule,
    PagesModule
  ],

查看 Github Issue.