【问题标题】:ChunkLoadError / Unhandled Promise rejection Ionic 5 /angularChunkLoadError / 未处理的承诺拒绝 Ionic 5 /angular
【发布时间】:2020-09-02 19:39:52
【问题描述】:

我正在尝试向我的 ionic 5 应用程序添加离线功能。为此,我实现了在连接时将信息存储在 sql lite 中的逻辑,并且在离线时从 sql lite 而不是 http 请求中获取数据。

  obtenerVisitaDetalle(idVisita: number): Observable<any> {

if (this.networkService.getCurrentNetworkStatus() == ConnectionStatus.Offline) {
  return from(this.getLocalData(`Visita/${idVisita}`));
} else {
  return this.http.get<any[]>(environment.UrlBaseApi + `Visita/${idVisita}`, this.httpOptions).pipe(
    tap(res => {
      this.setLocalData(`Visita/${idVisita}`, res);
    })
  )
}

}

到目前为止一切顺利,但我遇到了以下问题:

https://github.com/ionic-team/ionic-framework/issues/20859

基本上当我离线时,我无法打开像模态这样的组件,因为这些组件尚未预加载。评论上面提到的使用服务工作者提前预取您需要的块/资产的链接。

任何指导,例如如何实现这一点,我们将不胜感激。谢谢

编辑:根据第一个答案,我提供了部分路由文件。

  {
path: 'pago',
canActivate: [UserAuthenticatedGuard],
loadChildren: () => import('./Pages/pago/pago.module').then( m => m.PagoPageModule)
  }
];


@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})

【问题讨论】:

    标签: javascript angular typescript ionic-framework service-worker


    【解决方案1】:

    Angular Cli V6 支持预加载,因此您可能需要查看https://angular.io/api/router/PreloadAllModules。基本上,您可以通过将以下内容添加到 RouterModule 来预加载所有模块。

    RouterModule.forRoot(
       routes, { preloadingStrategy: PreloadAllModules ,}
    ),
    

    或者您可以通过定义自定义 PreloadingStrategy 来预加载特定路由。

    export class CustomPreloadingStrategy implements PreloadingStrategy { 
         preload(route: Route, load: Function): Observable<any> {  
             return route.path == 'YOUR_PATH' ? load() : of(null); 
         }
    }
    

    【讨论】:

    • 您好,感谢您抽出宝贵时间,请检查编辑。好像我已经在做它了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 2018-03-31
    • 2023-03-17
    • 2018-04-01
    • 2021-09-19
    相关资源
    最近更新 更多