Angular CLI ERROR in Cannot read property 'loadChildren' of null

前端 未结 11 2270
青春惊慌失措
青春惊慌失措 2021-01-01 12:24

I\'m converting a project to use angular cli and everything is working (once it\'s build) but i\'ve got a weird behaviour during build.

with ng serve I

11条回答
  •  温柔的废话
    2021-01-01 13:01

    For completeness: I ran into this issue when creating routes from a list of related objects:

    import { NgModule } from '@angular/core';
    import { RouterModule, Route } from '@angular/router';
    import { DashboardComponent } from './dashboard/dashboard.component';
    import { EXAMPLES, Example } from './examples-list';
    
    const routes = [
      ...EXAMPLES.map(toRoute), // this line produced the error
      { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
      { path: 'dashboard', component: DashboardComponent },
      { path: '**', redirectTo: '/dashboard', pathMatch: 'full' }
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    
    // `export`ing this function fixed the error
    export function toRoute(ex: Example): Route {
      return {
        path: ex.path,
        component: ex.component,
        data: { title: ex.title }
      };
    }
    

    I had to export the function I was using in map().

提交回复
热议问题