【问题标题】:Angular 5 : Lazy Loading is not working properly with ng build - -prod with Angular-CLI 1.7.xAngular 5:延迟加载无法与 ng build 一起正常工作 - -prod 与 Angular-CLI 1.7.x
【发布时间】:2018-08-09 19:33:40
【问题描述】:

我在下面工作:

  • 角度:5.2.6
  • Angular-CLI:1.7.x

我的应用下有这个路由文件(我有一些延迟加载模块)

const homeRoutes: Routes = [
  {
    path: 'home', component: HomeComponent,
    children: [
        ....
      {
        path: 'admin',
        loadChildren: 'app/home/admin/admin.module#AdminModule',
        canActivate: [AuthGuardAdmin]
      },
    ]
  },
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(homeRoutes)
  ],
  exports: [
    RouterModule
  ],
  declarations: []
})
export class HomeRoutingModule { }

运行 ng serve 时,延迟加载不起作用,我收到此错误:

>     core.js:1448 ERROR Error: Uncaught (in promise): TypeError: undefined is not a function
>     TypeError: undefined is not a function

通过谷歌搜索,我被告知要像这样更改我的延迟加载:

  {
    path: 'admin',
    loadChildren: ()=> AdminModule,
    canActivate: [AuthGuardAdmin]
  },

这在 development mode :) 下工作,但是在运行 ng build --prod 时,它再次失败并抛出这个:

ERROR in Error during template compile of 'HomeRoutingModule'
  Function expressions are not supported in decorators in 'ɵ0'
    'ɵ0' contains the error at app/home/home-routing/home-routing.module.ts(182,23)
      Consider changing the function expression into an exported function.

所以我担心我无法让它工作。

出于某些其他原因,我不想将 angular-cli 降级到 1.6.x

那么有什么解决方法的想法吗?

【问题讨论】:

  • 您使用什么typescript 版本?试试2.6.2
  • @messerbill ,我使用的是 2.4.2
  • This: loadChildren: ()=> AdminModule 不适用于延迟加载(或 AOT)。坚持你原来的语法,让我们找出导致错误的原因。
  • 你能发布一个重复的 repo,我不能用发布的代码复制
  • @firasKoubaa 更改为 2.6.2 版本不起作用吗?

标签: javascript angular typescript angular2-routing angular-cli


【解决方案1】:

解决方案很明显。

对我来说,这是删除 app.module 中延迟加载的模块导入。因为它应该是延迟加载的,所以这是有道理的。 在这个线程的某个地方找到了解决方案:https://github.com/angular/angular-cli/issues/9488 适用于 cli 版本 > 1.7.x

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    RouterModule,
    // custom modules
    AppRoutingModule,
    HeaderModule,
    LoginModule,
    Feature1Module, <!-- this is my lazyLoaded module // remove this line
    ConceptModule
  ],
  providers: [AuthService, AuthStoreService, AuthGuard],
  bootstrap: [AppComponent]
})
export class AppModule {
}

希望它可以帮助别人。

【讨论】:

  • 这对我很有帮助,我忘了停止在应用组件中导入我的“懒惰”模块。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 2021-07-17
  • 2016-09-27
相关资源
最近更新 更多