Ionic 4/Angular Router - how to route to a tab via a button/item?


#1

Hi everyone,

how is it possible to route to lazy loaded tabs via an ion-item? I created just the ionic 4 starter tabs template with one tab as a menu (called maintabs). Within the menu tab I have an ion-item which should open another tabs (called consumer-tabs). When clicking this item, nothing happens and the new route as well as the consumer tabs are not shown.

Does anyone have a solution?

Kind regards,
Thomas

Screenshot:

Here is my code:

App Routing:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', loadChildren: './pages/maintabs/maintabs.module#MaintabsModule' },
  { path: '', loadChildren: './pages/consumer/tabs/consumer-tabs.module#ConsumerTabsModule' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

export class AppRoutingModule { }

Maintabs Routing:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { MaintabsPage } from './maintabs.page';
import { MenuPage } from '../menu/menu.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: MaintabsPage,
    children: [
      {
        path: 'menu',
        outlet: 'menu',
        component: MenuPage
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/(menu:menu)',
    pathMatch: 'full'
  },
  {
    path: '**',
    redirectTo: '/tabs/(menu:menu)',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class MaintabsRouting { }

Maintabs html:

<ion-tabs tabbarPlacement="top">
  <ion-tab label="Home" icon="menu" href="/tabs/(menu:menu)">
    <ion-router-outlet name="menu"></ion-router-outlet>
  </ion-tab>
</ion-tabs>

Consumer Routing:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { ConsumerTabsPage } from './consumer-tabs.page';
import { ConsumerHomeListPage } from '../homepage/consumer-home-list/consumer-home-list.page';

const routes: Routes = [
  {
    path: 'consumer',
    component: ConsumerTabsPage,
    children: [
      {
        path: 'homepage',
        outlet: 'homepage',
        component: ConsumerHomeListPage
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class ConsumerTabsRouting { }

ConsumerTabs html:

<ion-tabs tabbarPlacement="top">
  <ion-tab label="Homepage" icon="menu" href="/consumer/(homepage:homepage)">
    <ion-router-outlet name="homepage"></ion-router-outlet>
  </ion-tab>
</ion-tabs>

Menu html:

<ion-content>
  <ion-card>
    <ion-item button class="cursor" href="/consumer/(homepage:homepage)">
      <ion-avatar slot="start">
        <img src="assets/images/Michael.jpg">
      </ion-avatar>
      <ion-label>
        <h2 class="text-bolder">Michael Baßler</h2>
        <p>@MichaelBaßler</p>
        <p>100,000 Followers</p>
      </ion-label>
    </ion-item>
  </ion-card>
</ion-content>

#2

I’m sorry, I can’t help you on it but… How did you installed ionic 4? Because I used the @canary tag from npm, but It seems it doesn’t install Angular 6 with it…


#3

Have a look here:


#4

That article fails to give the basic instructions to actually install anything. It says “…Ionic 4 Alpha is floating there somewhere…”.

I am on 3.x, what do I need to execute from the CLI to install anything Ionic 4.x ?
According to this link

“v4.0.0-alpha.8” is somehow accessible??

Thank you.


#5

Ionic 4 beta requires the --type=angular flag and version 4.0.0 or greater of the CLI.

Install the Ionic CLI globally with npm:
npm install -g ionic

The three most common starters are the blank starter, tabs starter, and sidemenu starter. Get started with the ionic start command:
ionic start myApp tabs --type=angular


#6

I think this is a bad UI. Tabs are for allowing the user to switch between unrelated subworlds of an app, and as such all navigation activity in the body of a tab should only change what is going on in that specific tab. The only way the app should move from tab A to tab B is when the user chooses tab B explicitly, not via any buttons in the body of a page.


#7

That’s not right. Have a look at Facebook or Youtube App. If you click a profile tabs will open e.g. homepage, photos, videos etc. That’s what I want. In other words, it would be nice to know how to implement nested tabs with Angular routing.


#8

Hey Thomas, I’m having the same issue and was wondering if you found a working way to do this?

Thank you!


#9

Use routes to navigate to the page containing the tabs definition and apply id to feed tab reference to tabs

Use tabs.select() method to select the appropriate tab to display

What I would try looking at the docs

No coded example

Tom


#10

I really would love to fix this problem, and there’s nothing in the docs about navigating from a page to a tabbed UI.

I basically want to navigate from a language selection page to the tabbed UI.

Any help would be really appreciated.

mypage.page.html (my homepage, I want to navigate from here to the tabbed UI)

<ion-content padding>
      <ion-button routerLink="tabs" routerDirection="forward"> Go to tabs </ion-button>
</ion-content>

app-routing.module.ts:

const routes: Routes = [
  { path: '', loadChildren: './select-language/select-language.module#SelectLanguagePageModule' },
  { path: 'tabs', loadChildren: './tabs/tabs.module#TabsPageModule' },
  { path: 'login', loadChildren: './login/login.module#LoginPageModule' },
];

Error I get:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'tabs'
Error: Cannot match any routes. URL Segment: 'tabs'

#11

AppRouting seems to be problem. Router will be confused will module to load for path “”.
Try changing it to
approuting:
const routes: Routes = [ { path: 'tabs', loadChildren: './pages/maintabs/maintabs.module#MaintabsModule' }, { path: 'consumer', loadChildren: './pages/consumer/tabs/consumer-tabs.module#ConsumerTabsModule' } ];

maintabs routing

const routes: Routes = [
  {
    path: '',
component: MaintabsPage,
    children: [
...
...

Consumer Routing:

const routes: Routes = [
  {
    path: 'consumer',
    component: ConsumerTabsPage,
    children: [
...
...

#12

Can you share routing of TabsPageModule?
it should have a path of “” and then children’s defined.


#13

Have you found the solution?. I’m also having this issue with Ionic 4 + Angular 6

Trying to link to a tabbed page from a menu link.

core.js:1673 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: ‘tabs’
Error: Cannot match any routes. URL Segment: ‘tabs’


#14

No, I didn’t find a solution. It can’t be that hard to navigate via a button to new Tabs. I think the whole Ionic Tabs Module with all this outlets are a bit crazy and not easy.


#15

I’ve been having the same issue. I was able to get tabs to work “correctly” if I had it set as the default route. I can navigate to a standard page, and navigate back to the tab page. Thinking about making some janky hack where it loads the tab page, does some form of validation check to determine what page to be on, then navigate to that page.

I’ll be keeping my eye on this thread to see if anyone figures out how to navigate to a tabs page.


#16

This is not the only issue I had with Ionic 4. Tbh I’ll move back again to Ionic 3 and I’ll wait for a release


#17

Same problem there! Any solution?


#18

It is actually working for me. I am using ionic angular 4 beta 13. The only thing is that only ion-item and ion-button can bu used.


#19

use similar way:
In app-routing.module.ts:
const routes: Routes = [
{ path: ‘tab’, loadChildren: ‘./tabs/tabs.module#TabsPageModule’ },
{ path: ‘’, loadChildren: ‘./login/login.module#LoginPageModule’ }
];

place include tab path and then load the tab module on button click:
<ion-button size=“small” href="**tab/**tabs/(home:home)>Click here

I hope, now problems will solve. Happy coding
Thanks