Ionic 4导航到选项卡

时间:2018-10-01 09:06:27

标签: ionic4 ionic-tabs

我正在研究Ionic 4项目,已经生成了一个tabs项目。

我要做的是创建一个登录页面,这是默认页面。

用户成功登录后,我要导航至选项卡。

当我尝试执行此操作时,出现错误:

Error: Cannot match any routes. URL Segment: 'tabs'

这些是我的路线:

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

在我的登录页面中,我有一个按钮,如下所示:

<ion-button expand="block" [href]="'tabs'" color="light" fill="outline">Sign in</ion-button>

当我生成其他页面时,我可以使用相同的方式导航到该页面。

3 个答案:

答案 0 :(得分:3)

I was facing the same issue. I found a solution here. You need to add an additional route to your routes array.

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

答案 1 :(得分:0)

第1步:在您的app-routing.module.ts

中向标签页添加其他路由
{ path: 'app', loadChildren: './pages/tabs/tabs.module#TabsPageModule' }

第2步:在tabs-routing.module.ts中添加标签路线

 const routes: Routes =[
   {
     path:'tabs',
     component:TabsPage,
     children:[
       {
         path : 'home',
         outlet : 'home',
         component : HomePage
       },
       {
         path : 'me',
         outlet : 'me',
         component : MePage
       }
     ]
   }
 ];

第3步:链接到标签页

 <ion-button href="app/tabs/(home:home)" routerDirection='root'>Tabs</ion-button>

答案 2 :(得分:0)

我遇到了同样的问题。默认情况下,我的第一页是“登录”页面。单击按钮后,我想导航到标签模块。

app-routing.module.ts

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

const routes: Routes = [
   { path: 'app', loadChildren: './tabs/tabs.module#TabsPageModule' },
   { path: '', loadChildren: './sign-in/sign-in.module#SignInPageModule' },
  { path: 'search', loadChildren: './search/search.module#SearchPageModule' }
];
@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  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';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'home',
        children: [
          {
            path: '',
            loadChildren: '../home/home.module#HomePageModule'
          }
        ]
      },
      {
        path: 'my-requests',
        children: [
          {
            path: '',
            loadChildren: '../my-requests/my-requests.module#MyRequestPageModule'
          }
        ]
      },
      {
        path: 'add-request',
        children: [
          {
            path: '',
            loadChildren: '../add-request/add-request.module#AddRequestPageModule'
          }
        ]
      },
      {
        path: 'search',
        children: [
          {
            path: '',
            loadChildren: '../search/search.module#SearchPageModule'
          }
        ]
      },
      {
        path: 'profile',
        children: [
          {
            path: '',
            loadChildren: '../profile/profile.module#ProfilePageModule'
          }
        ]
      },
      {
        path: '',
        redirectTo: '/tabs/home',
        pathMatch: 'full'
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/home',
    pathMatch: 'full'
  }
];

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

sign-in.module.ts

....

const routes: Routes = [
  {
    path: "",
    component: SignInPage
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild(routes)
  ],
  declarations: [SignInPage]
})

....

sign-in.page.html

 <ion-button (click)="navigateToProfile()">Sign In</ion-button>

sign-in.page.ts

navigateToProfile(){
  this.navController.navigateRoot(`app/tabs/home`);
  }

总体而言,我的解决方案是:

  • 在我的根模块app-routing.module中添加另一个路径:“ app”
  • 使用NavController使用路由导航到根目录。有关更多详细信息,请参见here,我在这里找到了。