延迟加载在Angular 6中不起作用

Cha*_*nka 6 angular

我在尝试导航到angualr中的' http:// localhost:4200/dashboard '延迟加载路径时遇到此错误,angualr-cli 6.0.0

错误错误:未捕获(在承诺中):错误:找不到模块"app/dashboard/dashboard.module".错误:找不到模块"app/dashboard/dashboard.module".at $ _lazy_route_resource lazy namespace object:5

const routes: Routes = [
    {
        path: 'login',
        loadChildren: 'app/login/login.module#LoginModule',

    },
    {
        path: '',
        component: MasterComponent,
        children: [
            {
                path: 'dashboard',
                loadChildren: 'app/dashboard/dashboard.module#DashboardModule'
            }
        ],
    },
    {
        path: '**',
        redirectTo: 'login
    }
];


@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule],
    providers: []
})
export class AppRoutingModule {
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*nka 10

在以前的版本中,使用'app/path/to/module#Module'的loadChildren路径支持,但它不再起作用,而不是使用相对路径'./path/to/module#Module'

angular/aio/content/examples/lazy-loading-ngmodules示例也在3天前更改了Angular 6发布

https://github.com/angular/angular/commit/f44a2c730a84cd86695d1851395ad28423704bd0

Angular社区一直在回答我提出的问题,请在下面找到答案.

https://github.com/angular/angular-cli/issues/10673#issuecomment-391786453

根据角度社区的回应,他们将更新文件.

用法需要改变

const routes: Routes = [
  {
    path: 'customers',
    loadChildren: 'app/customers/customers.module#CustomersModule'
  },
  {
    path: 'orders',
    loadChildren: 'app/orders/orders.module#OrdersModule'
  },
  {
    path: '',
    redirectTo: '',
    pathMatch: 'full'
  }
];
Run Code Online (Sandbox Code Playgroud)

const routes: Routes = [
  {
    path: 'customers',
    loadChildren: './customers/customers.module#CustomersModule'
  },
  {
    path: 'orders',
    loadChildren: './orders/orders.module#OrdersModule'
  },
  {
    path: '',
    redirectTo: '',
    pathMatch: 'full'
  }
];
Run Code Online (Sandbox Code Playgroud)

另一个提示:

模块导入顺序很重要 https://angular.io/guide/router#module-import-order-matters

  • 另一个解决方案:https://angular.io/guide/router#module-import-order-matters*周围的导入顺序.(将*RoutingModule移动到导入列表的底部)*还从NgModule.imports属性列表中删除了延迟加载的模块 (2认同)

Ale*_*lva 5

我遇到了同样的问题,但是在角度6中,似乎每个加载了“延迟加载”的模块都必须从imports根模块(app.module.ts)的声明中删除。至少对我来说有效。