I have an Angular CLI application with several lazy loadable modules, some of which contain their own routers. When navigating from the root of the application, I have no problems accessing all the routes. However, if I try to route directly to a route in a lazy loaded module, it turns out that the browser is trying to download the application from a folder that does not exist.
For example, the root routing module has routes that look something like this:
{
path: 'A', loadChildren:
'./A/A.module#AModule',
canActivate: [LoadGuardService]
}
AModule then has this for routes:
export const AModRoutes: Routes = [
{
path: '', component: AHomeComponent, children: [
{ path: 'building', pathMatch: 'full', component: BuildingComponent },
{ path: 'sitewide', pathMatch: 'full', component: SitewideComponent },
{ path: 'spaces', pathMatch: 'full', loadChildren: '../spaces/spaces.module#SpacesModule' }
]
}
];
If I download the application from scratch (running on port 4200), I can click and go to 127.0.0.1:4200/A/building
, however, I cannot directly go to the same URL.
The console gives a bunch of errors like this:
GET http://127.0.0.1:4200/es/inline.bundle.js net::ERR_ABORTED building:20
.
.
.
GET http://127.0.0.1:4200/es/main.bundle.js 404 (Not Found) build:20
, href - . CLI
{ provide: APP_BASE_HREF, useValue: '/' }
, .
- :
, , A/building
, A/sitewide
, B/
A/spaces
. CLI
EDIT: ( , )
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoadGuardService } from "./services/load-guard.service";
export const APPROUTES: Routes =
[
{
path: 'A', loadChildren:
'./A/A.module#AModule',
canActivate: [LoadGuardService]
},
{
path: 'B', loadChildren:
'./B/B.module#BModule',
canActivate: [LoadGuardService],
data: { byPhone: false }
},
...
];
@NgModule({
imports: [RouterModule.forRoot(APPROUTES)],
exports: [RouterModule],
providers: [LoadGuardService]
})
export class AppRoutingModule { }
, Routes .