How to set default login page and after login open

2020-03-07 05:30发布

问题:

I am creating ionic tabs project and I want to set in default login page then after open tabs . I am set in default login page but after login i am not getting tabs in ionic 4. Please help me how to resolve this problem....?

tabs.router.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',
        children: [
          {
            path: '',
            loadChildren: '../tab1/tab1.module#Tab1PageModule'
          }
        ]
      },
      {
        path: 'tab2',
        children: [
          {
            path: '',
            loadChildren: '../tab2/tab2.module#Tab2PageModule'
          }
        ]
      },
      {
        path: 'tab3',
        children: [
          {
            path: '',
            loadChildren: '../tab3/tab3.module#Tab3PageModule'
          }
        ]
      },
      {
        path: '',
        redirectTo: '/tabs/tab1',
        pathMatch: 'full'
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/tab1',
    pathMatch: 'full'
  }
];

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

app-routing.module.ts

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

const routes: Routes = [
  { path: '', redirectTo: 'login', pathMatch: 'full' },
  { path: 'tabs/tab1', loadChildren: './tabs/tabs.module#TabsPageModule' },
  { path: 'login', loadChildren: './login/login.module#LoginPageModule' },
  { path: 'register', loadChildren: './register/register.module#RegisterPageModule' }
];
@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule {}

I am changing the route but do not getting tabs plase help me how to add route to open tabs after login .

回答1:

This should work.

tabs.router.module.ts

...
const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'tab1',
        children: [
          { path: '', loadChildren: '../tab1/tab1.module#Tab1PageModule'}
        ]
      },
      ...
      { path: '', redirectTo: '/start/tabs/tab1', pathMatch: 'full'}
    ]
  }
];
...

app-routing.module.ts

...
const routes: Routes = [
  { path: '', redirectTo: 'login', pathMatch: 'full' },
  { path: 'start', loadChildren: './tabs/tabs.module#TabsPageModule' },
  { path: 'login', loadChildren: './login/login.module#LoginPageModule' },
];
...


回答2:

app-routing.module.ts

...

const routes: Routes = [
  { path: '', redirectTo: 'login', pathMatch: 'full' },
  { path: 'tabs', loadChildren: './tabs/tabs.module#TabsPageModule' },<--here
  { path: 'login', loadChildren: './login/login.module#LoginPageModule' },
];

tabs.router.module.ts

...

const routes: Routes = [
  {
    path: '',<--here
    component: TabsPage,
    children: [
      {
        path: 'tab1',
        children: [
          { path: '', loadChildren: '../tab1/tab1.module#Tab1PageModule'}
        ]
      },
      ...
      { path: 'tabs', redirectTo: '/tabs/tab1', pathMatch: 'full'}<--here
    ]
  }
];

...

navigate with: this.router.navigate(['tabs']);