[USING: Angular 8, Chrome79, Ionic 4, dev mode (ionic serve)]
I have an issue with my Angular Router, I have enabled PreloadAllModules
and depending which module come first in my routes definition it will cause ReferenceError: Cannot access 'myModule' before initialization
to the other.
import { NgModule } from "@angular/core";
import { PreloadAllModules, RouterModule, Routes } from "@angular/router";
const routes: Routes = [
{
path: "",
pathMatch: "full",
redirectTo: "home"
},
{
loadChildren: () => import("./home/home.module").then((m) => m.HomePageModule),
path: "home"
},
{
loadChildren: () => import("./list/list.module").then((m) => m.ListPageModule),
path: "list"
},
{
loadChildren: () =>
import("./myFirstModule/app/myFirstModule.module").then((m) => m.MyFirstModule),
path: "myFirstModule"
},
{
loadChildren: () =>
import("./mySecondModule/app/mySecondModule.module").then((m) => m.mySecondModule),
path: "mySecondModule"
}
];
@NgModule({
exports: [RouterModule],
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })]
})
export class AppRoutingModule {}
The above code will fire : ReferenceError: Cannot access 'mySecondModule' before initialization
when clicking on the link in my menu, BUT the error will not fire if I access the route directly by URL !
If I intervert myFirstModule
and mySecondModule
like so :
const routes: Routes = [
{
path: "",
pathMatch: "full",
redirectTo: "home"
},
{
loadChildren: () => import("./home/home.module").then((m) => m.HomePageModule),
path: "home"
},
{
loadChildren: () => import("./list/list.module").then((m) => m.ListPageModule),
path: "list"
},
{
loadChildren: () =>
import("./mySecondModule/app/mySecondModule.module").then((m) => m.mySecondModule),
path: "mySecondModule"
},
{
loadChildren: () =>
import("./myFirstModule/app/myFirstModule.module").then((m) => m.MyFirstModule),
path: "myFirstModule"
}
];
The error will fire on navigation to myFirstModule
: ReferenceError: Cannot access 'myFirstModule' before initialization
but it will work if I access directly via URL ....
If I remove PreloadAllModules I can navigate a first time to any module, but then when I access the other module I get an error of my libraries :
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'prototype' of undefined
TypeError: Cannot read property 'prototype' of undefined at sax.js:222
myFirstModule
export function createTranslateLoader(http: HttpClient): TranslateHttpLoader {
return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
}
@NgModule({
declarations: [
MyFirstModuleComponent,
...
],
entryComponents: [
...
],
exports: [],
imports: [
CommonModule,
IonicModule,
MyMaterialModule,
MyStore1Module,
RouterModule.forChild([
{
component: MyFirstModuleComponent,
path: ""
}
]),
TranslateModule.forChild({
loader: {
deps: [HttpClient],
provide: TranslateLoader,
useFactory: createTranslateLoader
}
})
],
providers: [MyService]
})
export class MyFirstModule{}
MySecondModule
registerLocaleData(_default, "fr");
@NgModule({
declarations: [
MySecondModuleComponent,
...
],
entryComponents: [
...
],
imports: [
CommonModule,
MyMaterialModule,
MatDialogModule,
ClickOutsideModule,
MyStore2Module,
MyStore3Module,
AngularSplitModule.forChild(),
HighchartsChartModule,
RouterModule.forChild([
{
component: MySecondModuleComponent,
path: ""
}
])
],
providers: [
{
provide: LOCALE_ID,
useValue: "fr-FR"
}
]
})
export class MySecondModule{}
I found a solution.
A coworker imported sax without npm and that caused xml2js and jszip to conflict over this library.
I installed sax via npm and removed the manual import and it is working now.