Hello, I'm working with Angular where I have a structure like this:
|-app
|-fooModule (fooPage1, fooPage2)
|-barModule (barPage1, barPage2)
These two modules are being loaded using lazyLoad
in the route file as follows:
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: 'bar', loadChildren: './foo/foo.module#FooModule' },
{ path: '' , loadChildren: './bar/bar.module#BarModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
The AppRoutingModule
module is loaded in the initial project module, AppModule
together with others, and both foo and bar the same structure, instead of loading modules with lazyLoad, I load components like this:
example: foo-routing.module (bar-routing.module.ts has the same structure)
import { FooPage1Component } from './foo-page-1/foo-page-1.component';
import { FooPage2Component } from './foo-page-2/foo-page-2.component';
const routes: Routes = [
{ path: 'page2', component: FooPage2Component },
{ path: '' , component: FooPage1Component }
];
My problem is that when I'm on the route www.meusite.com/page2 and try to force a route to BarModule, Angular does not remove the already loaded components from FooPage2Component. It's like he does not update on <router-outlet>
. The way I call another module can be seen in the FooPage2Component component:
foo-page-2.component
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'foo-page-2',
templateUrl: './foo-page-2.component.html',
styleUrls: ['./foo-page-2.component.scss']
})
export class FooPage2Component {
constructor(
private router:Router
){
// depois de 2 segundos, redirecione para o módulo bar na pagina 2
setTimeout(()=> router.navigate(['../bar/page2']), 2000);
}
}
When I compile the code above, and do the test, the result on my page when doing the redirection looks like this:
browser rendered structure
<html>
<head>
</head>
<body>
<app>
<router-outlet></router-outlet>
<foo-page-2></foo-page-2>
<bar-page-2></bar-page-2>
</app>
</body>
</html>
So here's the question: if I have 2 different modules that have their own routes and are loaded dynamically (with lazyLoad in loadChildren) how can I tell how angular I want, from the FooPage2 component that is in FooModule, redirect to a BarPage2 component in BarModule?
Note: I've tried using Router.navigate , Router.navigateByUrl , I already tried to force navigation by passing the NavigationExtra.relativeTo and even then, it does not work. Unfortunately my solution so far is being a (horrible)
window.location.href = window.location.href.replace('/page2','/bar/page2)
;
Can anyone help me?