Ionic 4 / Angular 6 :选项卡中的嵌套子路由

Ionic 4 / Angular 6 : Nested child routes in tabs

我使用 Ionic Tabs 组件生成了一个应用程序。

选项卡是交货关于联系

然后我生成了一个页面详情

我希望页面 详细信息 成为 交付 选项卡的子项。

意思是当我在 详细信息 页面上时,我正在浏览 交付 选项卡。

有人在

上问过类似的问题

生成的目录结构:

- about
- contact
- deliveries
    deliveries.module.ts
- details
    details.module.ts
- tabs
    tabs.module.ts
    tabs.page.html
    tabs.router.module.ts
app.module.ts
app-routing.module.ts

这是生成的app-routing.module.ts

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

const routes: Routes = [
  { path: '', loadChildren: './tabs/tabs.module#TabsPageModule' },
];
@NgModule({
  imports: [
    RouterModule.forRoot(routes)
  ],
  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';
import { DeliveriesPage } from '../deliveries/deliveries.page';
import { AboutPage } from '../about/about.page';
import { ContactPage } from '../contact/contact.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: '',
        redirectTo: '/tabs/(deliveries:deliveries)',
        pathMatch: 'full',
      },
      {
        path: 'deliveries',
        outlet: 'deliveries',
        component: DeliveriesPage
      },
      {
        path: 'about',
        outlet: 'about',
        component: AboutPage
      },
      {
        path: 'contact',
        outlet: 'contact',
        component: ContactPage
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/(deliveries:deliveries)',
    pathMatch: 'full'
  }
];

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

这是生成的tabs.page.html

<ion-tabs>
  <ion-tab label="Deliveries" icon="bicycle" href="/tabs/(deliveries:deliveries)">
    <ion-router-outlet name="deliveries"></ion-router-outlet>
  </ion-tab>
  <ion-tab label="About" icon="information-circle" href="/tabs/(about:about)">
    <ion-router-outlet name="about"></ion-router-outlet>
  </ion-tab>
  <ion-tab label="Contact" icon="contacts" href="/tabs/(contact:contact)">
    <ion-router-outlet name="contact"></ion-router-outlet>
  </ion-tab>
</ion-tabs>

这是生成的deliveries.module.ts

import { IonicModule } from '@ionic/angular';
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { DeliveriesPage } from './deliveries.page';

@NgModule({
  imports: [
    IonicModule,
    CommonModule,
    FormsModule,
    RouterModule.forChild([
      {
        path: '',
        component: DeliveriesPage
      }
    ])
  ],
  declarations: [
    DeliveriesPage
  ]
})
export class DeliveriesPageModule {}

这是details.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';

import { IonicModule } from '@ionic/angular';

import { DetailsPage } from './details.page';

const routes: Routes = [
  {
    path: '',
    component: DetailsPage,
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild(routes)
  ],
  declarations: [DetailsPage]
})
export class DetailsPageModule {}

所以,我所有的尝试都失败了,我尝试遵循与上面相同的逻辑。

我尝试在 deliveries.module.ts

工作
RouterModule.forChild([
  {
    path: '',
    component: DeliveriesPage,
  },
  {
    path: 'details',
    loadChildren: '../details/details.module#DetailsPageModule'
  }
]),

我也试过了

RouterModule.forChild([
  {
    path: '',
    component: DeliveriesPage,
  },
  {
    path: 'details',
    outlet: 'deliveries
    loadChildren: '../details/details.module#DetailsPageModule'
  }
]),

RouterModule.forChild([
  {
    path: '',
    component: DeliveriesPage,
    children: [
      {
        path: 'details',
        loadChildren: '../details/details.module#DetailsPageModule'
      }
    ]
  },
]),

找不到从

访问详细信息页面的方法

有可能实现吗?有点混乱。

现在访问 详细信息 页面的简单方法是在 app-routing.module.ts,但它不会是同一个路由器插座的一部分。

这是迄今为止我发现的最简洁的方法。

这里是tabs.router.module.ts

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

import { TabsPage } from './tabs.page';
import { DeliveriesPage } from '../deliveries/deliveries.page';
import { AboutPage } from '../about/about.page';
import { ContactPage } from '../contact/contact.page';
import { DetailsPage } from '../details/details.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: '',
        redirectTo: '/tabs/(deliveries:deliveries)',
        pathMatch: 'full',
      },
      {
        path: 'deliveries',
        outlet: 'deliveries',
        component: DeliveriesPage,
      },
      {
        path: 'details/:id',
        outlet: 'deliveries',
        component: DetailsPage
      },
      {
        path: 'about',
        outlet: 'about',
        component: AboutPage
      },
      {
        path: 'contact',
        outlet: 'contact',
        component: ContactPage
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/(deliveries:deliveries)',
    pathMatch: 'full'
  }
];

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

tabs.module.ts

中导入DetailsModulePage
import { IonicModule } from '@ionic/angular';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { TabsPageRoutingModule } from './tabs.router.module';

import { TabsPage } from './tabs.page';
import { ContactPageModule } from '../contact/contact.module';
import { AboutPageModule } from '../about/about.module';
import { DeliveriesPageModule } from '../deliveries/deliveries.module';
import { DetailsPageModule } from '../details/details.module';

@NgModule({
  imports: [
    IonicModule,
    CommonModule,
    FormsModule,
    TabsPageRoutingModule,
    DeliveriesPageModule,
    AboutPageModule,
    ContactPageModule,
    DetailsPageModule
  ],
  declarations: [TabsPage]
})
export class TabsPageModule {}

这里是 DetailsModulePage(非常基本)

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';

import { IonicModule } from '@ionic/angular';

import { DetailsPage } from './details.page';

const routes: Routes = [
  {
    path: '',
    component: DetailsPage,
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild(routes)
  ],
  declarations: [DetailsPage]
})
export class DetailsPageModule {}

这是访问详情页面的方法

  • /tabs/(交付:details/2)

如果你有更好的方法,我会很高兴知道。

我就是这样做的。在tabs.router.module.ts,

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'featured',
        children: [
          {
            path: '',
            loadChildren: '../tab-featured/tab-featured.module#TabFeaturedPageModule'
          }
        ]
      },
      {
        path: 'featured/:id',
        children: [
          {
            path: '',
            loadChildren: '../tab-featured-detail/tab-featured-detail.module#TabFeaturedDetailPageModule'
          }
        ]
      },
      {
        path: 'categories',
        children: [
          {
            path: '',
            loadChildren: '../tab-category/tab-category.module#TabCategoryPageModule'
          }
        ]
      },
      {
        path: 'popular',
        children: [
          {
            path: '',
            loadChildren: '../tab-popular/tab-popular.module#TabPopularPageModule'
          }
        ]
      },
      {
        path: '',
        redirectTo: '/tabs/featured',
        pathMatch: 'full'
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/featured',
    pathMatch: 'full'
  }
];

选项卡-精选-detail.module.ts

import { IonicModule } from '@ionic/angular';
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { TabFeaturedDetailPage } from './tab-featured-detail.page';

@NgModule({
  imports: [
    IonicModule,
    CommonModule,
    FormsModule,
    RouterModule.forChild([{ path: '', component: TabFeaturedDetailPage }])
  ],
  declarations: [TabFeaturedDetailPage]
})
export class TabFeaturedDetailPageModule {}

4.0.0-beta.18 ion-tab 被删除,没有必要使用命名插座。

演示(有两种不同的方法)+ 解释:
https://github.com/servrox/demo-ionic-tab-routing

Ionic CLI 版本 4.10.3
离子框架@ionic/angular 4.0.1

这一切让我感到非常困惑。 然后我意识到我在 Tabs Routing 中所要做的就是。

const routes: Routes = [
  { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
  { path: '',
    component: TabsPage,
    children: [
      { path: 'tab1', loadChildren: '../tab1/tab1.module#tab1Module'},
      { path: 'tab2', 
        children: [
          { path: '', loadChildren: '../tab2/tab2.module#tab2Module'},
          { path: ':ID', loadChildren: '../tab2/tab2details.module#tab2detailsModule'},
        ]
      },
    ]
  },
];

其中 Tab2 有一个列表页和一个详细信息页。

列表页面 URL:/tabs/tab2

详细信息页面 URL:/tabs/tab2/123/

当您在列表或详细信息页面时,Tab2 选项卡保持活动状态,当您在详细信息页面时,返回按钮显示。