2
Lazyloading modules are only loaded after I re-load the page.
In an application I have a problem: when I log in successfully, I am redirected to a route called 'Dashboard', but from there when I try to access any lazy module, this module is not loaded, no error occurs, the URL changes the title attribute peacefully, but the component is not loaded on the screen. Everything goes back to normal if I reload the page (F5), then I can access the lazy modules.
Summing up: When I soon in the application, if I click on any link that takes me to a route that loads a lazy module, it is not loaded.
No errors occur in the console, and when I try to access these lazy modules in the network tab no request is made for Chunck.
The Chunks are being generated perfectly.
Below my main route file:
export const routes: Routes = [
{
path: 'dashboard',
component: SistemaComponent,
canActivate: [SistemaLiberadoGuard,
SistemaGuard],
data: {
title: 'dashboard'
}
},
{
path: 'login',
component: S4LoginComponent,
canActivate: [ LoginGuard ],
data: {
title: 'login'
}
},
{
path: 'usuarios',
loadChildren: './usuario-perfil-permissoes/usuario-perfil-permissoes.module#UsuarioPerfilPermissoesModule'
},
{
path: 'liberacaosistema',
loadChildren: './liberacao-sistema/liberacao-sistema.module#LiberacaoSistemaModule',
},
{
path: 'profileusuario',
loadChildren: './profile/profile.module#ProfileModule',
},
{
path: '',
pathMatch: 'full',
redirectTo: 'dashboard'
},
{
path: '**',
component: S4NotFoundComponent,
data: {
title: 'Page Not Found'
}
}
]
@NgModule({
imports: [
RouterModule.forRoot(routes, { useHash: true })
],
exports: [RouterModule]
})
export class AppRoutingModule {
}
Below the file of routes of one of the modules that are being lazily loaded:
export const routes: Routes = [
{
path: '',
component: ProfileComponent,
canActivate: [SistemaGuard],
data: {
title: 'Profile'
}
}
]
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [RouterModule]
})
export class ProfileRoutingModule {
}
Below my app.module
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpClientModule,
AccordionModule,
ButtonModule,
ScrollPanelModule,
BrowserAnimationsModule,
CoreModule,
SistemaModule,
LoadingModule,
AppRoutingModule,
ErrorsModule,
],
declarations: [
AppComponent,
AppMenuComponent,
AppSubMenuComponent,
],
providers: [
{provide: LocationStrategy, useClass: HashLocationStrategy},
],
bootstrap: [AppComponent]
})
export class AppModule { }
Below my login method :
login() {
const userName = this.loginForm.get('userName').value;
const password = this.loginForm.get('password').value;
this.autenticacaoService.autenticar(userName, password)
.subscribe(() => {
this.router.navigate(['', 'dashboard']);
},
err => {
this.showMessage('error', 'dados de login inválidos');
this.loginForm.reset();
this.platformDetectorService.isPlatformBrowser() && this.userNameInput.nativeElement.focus();
})
}
Hi Lucio can edit your question by adding your app.module?
– Eduardo Vargas
If you take the Guard and try to access it works??
– Eduardo Vargas
No. I have tried too, what happens is that everything works normally if I re-load the page
– Lucio Leandro
it seems to me that your route configuration is right. Edits with the code of how you do to navigate to page.
– Eduardo Vargas
I updated, Eduardo. I put my login method
– Lucio Leandro