I have a Angular 6 / Ionic 4 lazy load tabs template that works correctly. The main page has one router-outlet and the tabs have secondary named router-outlets. However, I cannot create a routerLink in the main menu that displays a tab page.
This routerLink works only if NOT already on a tab page. e.g.
- If on test page: /test
- Click link
Correctly links to: tabs/(about:about)
Link to tab page
If already on the home tab (and click the link) the final url is:
/tabs/(about:about//home:home)
How do I create a routerLink that always links to the following?
/tabs/(about:about)
I get the same behavior with this typescript:
this.router.navigate(['/tabs', { outlets: { about: ['about']}}]);
However, this typescript works correctly:
this.router.navigateByUrl('/tabs/(about:about)');
app.component.html
<ion-app>
<nav>
<a [routerLink]="['/tabs', { outlets: { about: 'about' } }]">
This link only works if NOT already on tab page.
</a>
<ion-button (click)="navigateByUrl()">This always works!</ion-button>
</nav>
<router-outlet></router-outlet>
</ion-app>
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', loadChildren: './pages/tabs/tabs.module#TabsPageModule' },
{ path: 'test', loadChildren: './pages/test/test.module#TestPageModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes, { enableTracing: true } )],
exports: [RouterModule]
})
export class AppRoutingModule {}
tabs.router.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TabsPage } from './tabs.page';
import { HomePage } from '../home/home.page';
import { AboutPage } from '../about/about.page';
import { ContactPage } from '../contact/contact.page';
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
{
path: 'home',
outlet: 'home',
component: HomePage
},
{
path: 'about',
outlet: 'about',
component: AboutPage
},
{
path: 'contact',
outlet: 'contact',
component: ContactPage
}
]
},
{
path: '',
redirectTo: '/tabs/(home:home)',
pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class TabsPageRoutingModule {}