Search code examples
angularionic-frameworkangular-routingionic5

Ionic 5 angular navigation: ion-back-button not visible


i have an app with a structure similar to the one I created on this stackblitz. the app have this three routes:

/tabs/tab1

/tab/tab2

/tab/tab2/subtab

I cannot understand If I'm missing something or if this is the navigation system, but when i go from /tab/tab2 to /tab/tab2/subpage I can see the back button. When I go from /tabs/tab1 to /tab/tab2/subpage it is not visible. Is it normal? I would like to see a back button to come back to /tab/tab1.
Thanks


Solution

  • Try this

    tab1.html,tab2.html

    <ion-button routerLink="/subpage"></ion-button>

    app-routing.module.ts

    import { NgModule } from "@angular/core";
    import { PreloadAllModules, RouterModule, Routes } from "@angular/router";
    
    const routes: Routes = [
      {
        path: "",
        loadChildren: () => import("./tabs/tabs.module").then(m => m.TabsPageModule)
      },
      {
        path: "subpage",
        loadChildren: () =>
          import("./subpage/subpage.module").then(m => m.SubpagePageModule)
      }
    ];
    @NgModule({
      imports: [
        RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
      ],
      exports: [RouterModule]
    })
    export class AppRoutingModule {}
    

    tabs-routing.module.ts

    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    import { TabsPage } from './tabs.page';
    
    const routes: Routes = [
      {
        path: 'tabs',
        component: TabsPage,
        children: [
          {
            path: 'tab1',
            loadChildren: () => import('../tab1/tab1.module').then(m => m.Tab1PageModule)
          },
    
          {
            path: 'tab2',
            loadChildren: () => import('../tab2/tab2.module').then(m => m.Tab2PageModule)
          },
          {
            path: '',
            redirectTo: '/tabs/tab1',
            pathMatch: 'full'
          }
        ]
      },
      {
        path: '',
        redirectTo: '/tabs/tab1',
        pathMatch: 'full'
      }
    ];
    
    @NgModule({
      imports: [RouterModule.forChild(routes)],
    })
    export class TabsPageRoutingModule {}