我在一个使用Ion Tabs的Angular 应用程序中面临着一个路由问题.我的应用程序在Ion Tabs中有一个"Home"标签,当用户点击"Home"标签中的一个项目时,我想允许导航到不同的页面.

我的路由 struct 如下:

在我的"主页"中,我有一个卡片列表,我希望当用户点击一张卡片时,他们会被重定向到带有特定ID的"用户-培训师-查看"页面.

"主页"标签在Ion Tabs中,但当我从主页导航到"User-Trader-view"时,Ion Tabs栏从视图中消失.

这是我在我的"家"里处理导航的方式.

goToUserTrainerView(id: any) {
  this.router.navigate(['/tabs/user-trainer-view', id]);
}

以下是我的路由定义:

在ionic 选项卡模块(tabs.module.ts)中:

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

import { TabsPage } from './tabs.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'home',
        loadChildren: () =>
          import('../trainers/home/home.module').then((m) => m.HomePageModule),
      },
      {
        path: 'trainer-user-view/:id',
        loadChildren: () => import('../trainers/user-trainer-view/user-trainer-view.module').then((m) => m.UserTrainerViewPageModule),
      }
    ],

  },
  {
    path: '',
    redirectTo: 'tabs/home',
    pathMatch: 'full',
  }
];

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

当我从主页导航到"user-trainer-view"页面时,我的路由配置有什么问题导致Ion选项卡栏消失?如何解决此问题并保持ionic 标签栏可见?

HomePage.html

<ion-header [translucent]="true">
  <!-- Header content here -->
</ion-header>

<ion-content [fullscreen]="true">
  <div class="flexTemplate" style="overflow: scroll;">
    <h3 class="textTitle">Your subscribers</h3>

    <div #cards>
      <ion-card *ngFor="let cardData of cardDataArray"
        (click)="goToUserTrainerView(cardData.id)" class="userCard">
        <div class="pholderImg" style="margin-left: 5px;">
          <img src="{{cardData.imgUri}}">
        </div>
        <ion-card-header>
          <ion-card-title>{{cardData.username}}</ion-card-title>
        </ion-card-header>
      </ion-card>
    </div>
  </div>
  
    
</ion-content>

推荐答案

我假设你有:

Initial Setup:
App
|__TabsPage
   |__HomePage
      |__Card (OnClick: Navigate to UserTrainerView with ID)
          
Navigation:
App
|__TabsPage
   |__UserTrainerViewPage (with ID, but TabsBar is missing)

据我所知,当您从HomePage导航到UserTrainerViewPage时,您实际上是leaving导致ionic 标签条消失的TabsPage上下文(当working with tabs in ionic时).

因此,try 修改您的goToUserTrainerView方法,使路径中的/tabs/前缀使用navigate,以确保您保持在TabsPage上下文中.

goToUserTrainerView(id: any) {
  this.router.navigate(['/tabs/trainer-user-view', id]);
}

由于您已经在tabs.module.ts中为UserTrainerViewPage定义了路由,因此您不需要在app-routing.module.ts中再次定义它.

// Remove this block from app-routing.module.ts
{
    path: 'user-trainer-view/:id',
    loadChildren: () => import('./trainers/user-trainer-view/user-trainer-view.module').then((m) => m.UserTrainerViewPageModule),
}

建议的设置为:

App
|__TabsPage
   |__HomePage
      |__Card (OnClick: Navigate to /tabs/trainer-user-view/{ID})
   |__UserTrainerViewPage (with ID, TabsBar is visible)

使用最新的home.ts:

goToUserTrainerView(id: any) {
  this.router.navigate(['/tabs/trainer-user-view', id]);
}

和你最新的tabs.module.ts名:

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'home',
        loadChildren: () =>
          import('../trainers/home/home.module').then((m) => m.HomePageModule),
      },
      {
        path: 'trainer-user-view/:id',
        loadChildren: () => import('../trainers/user-trainer-view/user-trainer-view.module').then((m) => m.UserTrainerViewPageModule),
      }
    ],

  },
  {
    path: '',
    redirectTo: 'tabs/home',
    pathMatch: 'full',
  }
];

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

同样,从app-routing.module.ts中删除冗余路由.


一些额外的判断:

确保在导航方法中使用绝对路径.

goToUserTrainerView(id: any) {
  this.router.navigate(['/tabs/trainer-user-view', id]);
}

还要确保您的user-trainer-view页与您的选项卡页加载在相同的<ion-router-outlet>页内.这样,选项卡栏应该保持可见.

<ion-router-outlet></ion-router-outlet>

在导航到第user-trainer-view页时,查看Ionic生命周期事件以查看是否有影响选项卡栏可见性的因素可能会很有帮助.

ionViewWillEnter() {
  console.log('Page is about to enter');
}
ionViewDidEnter() {
  console.log('Page has entered');
}

判断是否有任何可能隐藏选项卡栏的css样式.

Angular相关问答推荐

tabindex on button on button in MatMenu in angular不工作

当try 使用Angular和PrimeNg手动聚焦输入时,我做错了什么?

导致无限请求的Angular HttpInterceptor和HttpClientModule

如何在构造函数中测试MatDialog.Open

Angular 15:将数据从主零部件传递到辅零部件

筛选器不会从文本输入触发更改事件

列表和映射的SCSS语法

当快速拖动光标时会丢失元素 - Angular

如何将 Angular 中的导航路由到同一页面并重新加载

具有两个订阅方法的 Angular 16 takeUntilDestroyed 运算符

从 angular 11 升级到 angular 12 后,我的项目没有使用优化参数进行编译

*ngIf 和 *ngFor 在 元素上使用

Angular2 Material Dialog css,对话框大小

如何在angular material中使用输入类型文件

visibility:hidden

请求 http.GET 时发送的 Angular2 OPTIONS 方法

Angular-CLI 代理到后端不起作用

如何将一个组件放入Angular2中的另一个组件中?

Lint 错误:实现生命周期挂钩接口

Firestore 从集合中获取文档 ID