本文介绍了`loadChildren:()=>有什么区别? import('./hoge.module.ts).then(m => m.HogeModule)'和loadChildren:'./hoge.module#HogeModule'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

在以下结构的情况下,我们会显示错误cannot find module.

We get an error cannot find module in case following structure.

const routes: Routes = [
  {
    path: CHILD_MANAGEMENT_PORTAL.baseUrl,
    canActivate: [AuthGuard],
    component: EnvelopeComponent,
    loadChildren: () =>
      import('./features/child-management/child-management.module').then(
        m => m.ChildManagementModule
      ),
    data: {
      menuResolver: ChildManagementMenuResolver,
      pageTitleResolver: ChildManagementPageTitleResolver,
      portalData: CHILD_MANAGEMENT_PORTAL
    }
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}
const routes: Routes = [
  {
    path: 'dashboard',
    loadChildren: './dashboard/child-dashboard.module#ChildDashboardModule'
  },
  {
    path: '**',
    redirectTo: 'dashboard'
  }
];

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

我们可以通过仅将子routing.module的loadChildren从loadChildren: './hoge.module#HogeModule'更改为loadChildren: () => import('./hoge.module.ts).then(m => m.HogeModule)'来解决此错误.

We could solve this error by only changing loadChildren of child routing.module from loadChildren: './hoge.module#HogeModule' to loadChildren: () => import('./hoge.module.ts).then(m => m.HogeModule)'.

const routes: Routes = [
  {
    path: 'dashboard',
    loadChildren: () => import('./dashboard/child-dashboard.module').then(m => m.ChildDashboardModule)
  },
  {
    path: '**',
    redirectTo: 'dashboard'
  }
];

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

但是我不明白为什么. (我没有更改app-routing.module.ts ...)

But I could not understand why. (I did not change app-routing.module.ts...)

那么您能解释一下区别吗?

So could you explain the difference?

推荐答案

似乎您是从Angular 7.x升级到8.x的,这就是方案更改的地方.

It seems like you upgraded from Angular 7.x to 8.x and this is where the scheme changed.

说明(来自角度文档)

在版本8中,不建议使用loadChildren路由规范的字符串语法,而推荐使用import()语法的新语法.

In version 8, the string syntax for the loadChildren route specification was deprecated, in favor of new syntax that uses import() syntax.

之前

const routes: Routes = [{
  path: 'lazy',
  // The following string syntax for loadChildren is deprecated
  loadChildren: './lazy/lazy.module#LazyModule'
}];

之后

const routes: Routes = [{
  path: 'lazy',
  // The new import() syntax
   loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)
}];

希望这对您有所帮助.

这篇关于`loadChildren:()=>有什么区别? import('./hoge.module.ts).then(m => m.HogeModule)'和loadChildren:'./hoge.module#HogeModule'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:48