Search code examples
javascriptangularionic4angular9

after login when click hardware back button navigated to login page how to prevent it in ionic 4


const routes: Routes = [
  {
    path: '',
    component: TabsPage,
    children: [
      //new page add
      {
        path: 'home',
        children: [
          {
            path: '', loadChildren: () => import('../AllPages/home/home.module').then(m => m.HomePageModule)
          },
          {
            path: 'new-arrival',
            children: [
              {
                path: '',
                loadChildren: () => import('../AllPages/new-arrival/new-arrival.module').then(m => m.NewArrivalPageModule)
              },

              {
                path: 'product-item-list',
                children: [
                  {
                    path: '',
                    loadChildren: () => import('../Allpages/product-item-list/product-item-list.module').then(m => m.ProductItemListPageModule)
                  },

                  {
                    path: 'product-details',
                    children: [
                      {
                        path: '',
                        loadChildren: () => import('../Allpages/product-details/product-details.module').then(m => m.ProductDetailsPageModule)
                      },
                    ]

                  }
                ]
              }


            ]
          },

Solution

  • Use Ionic NavController instead of Angular Router.
    Then, use the navigateRootmethod.

    Going root means that all existing pages in the stack will be removed, and the navigated page will become the single page in the stack.

    import { NavController } from '@ionic/angular';
    ...
    constructor(private navController: NavController) {}
    ...
    this.navController.navigateRoot('/tabs/home');