【问题标题】:How can I hide and show ion-tabs conditionally in ionic2?如何在 ionic2 中有条件地隐藏和显示离子标签?
【发布时间】:2017-08-18 19:20:13
【问题描述】:

我有 5 个标签

<ion-tabs tabsPlacement="bottom" tabsLayout="icon-top" tabsHighlight="true" color="primary">
  <ion-tab [root]="myCardsTabRoot" tabTitle="My Cards" tabIcon="ios-photos"></ion-tab>
  <ion-tab [root]="calendarTabRoot" tabTitle="Calendar" tabIcon="calendar"></ion-tab>
  <ion-tab [root]="myProfileTabRoot" tabTitle="My Profile" tabIcon="ios-person"></ion-tab>
  <ion-tab [root]="searchTabRoot" tabTitle="Search" tabIcon="ios-search"></ion-tab>
  <ion-tab [root]="pendingRequestsTabRoot" tabTitle="Follow Req" tabIcon="notifications" tabBadge="{{pendingRequests}}" tabBadgeStyle="danger"></ion-tab>
</ion-tabs>

我只想在有任何待处理请求时才显示最后一个选项卡,我在下面尝试了但仍然没有机会,并且当pendingRequests 值发生变化时不显示/隐藏选项卡, pendingRequests 的值在 ionViewDidEnter() 中更改

  <div *ngIf="pendingRequests > 0">
    <ion-tab [root]="pendingRequestsTabRoot" tabTitle="Follow Req" tabIcon="notifications" tabBadge="{{pendingRequests}}" tabBadgeStyle="danger"></ion-tab>
  </div>

和

<ion-tab *ngIf="pendingRequests > 0" [root]="pendingRequestsTabRoot" tabTitle="Follow Req" tabIcon="notifications" tabBadge="{{pendingRequests}}" tabBadgeStyle="danger"></ion-tab>

如果我重新启动应用程序,选项卡会根据 pendingRequests 的值正确显示,唯一的问题是当值更改时,选项卡不会相应地隐藏/显示

这里是更新pendingRequests的代码

  ionViewDidEnter() {
    this.refreshPrendingRequests();
  }

  refreshPrendingRequests() {
    this.contactsService.getContactRequestsCount(this.userInfoService.currentProfile.id)
      .subscribe(count => this.pendingRequests = count);
  }

更新

根据@Sampath 的建议,我为它创建了一个 plunker。这种行为与我在我的应用程序上的行为不同,但这个行为也不起作用。

单击右上角的工具栏按钮应显示/隐藏选项卡 http://plnkr.co/edit/Cx6Pyr9AihvX66ASed0s?p=preview

【问题讨论】:

  • 能否显示pendingRequests代码sn-p的值变化?
  • 调试代码时能看到什么?价值变化是如何发挥作用的?
  • @Sampath 我在工具栏上添加了另一个按钮,该按钮隐藏和显示正确,因此这意味着更改值工作正常,甚至角度绑定工作正常
  • 任何控制台错误或类似的错误?
  • @Sampath 感谢您的帮助

标签: ionic-framework ionic2 ionic2-tabs


【解决方案1】:

如果您按照以下步骤操作,您可以这样做。代码是不言自明的。如果您需要任何帮助,请在下方评论。

注意:这只是一个工作示例。希望您可以轻松地将其转换为您的用例。

如果你克隆Git Repo here,你可以玩这个。

我在这个示例中使用了 Ionic2 Event publish/subscribe pattern

第一步:创建另一个标签页TabSet1Page

.ts

@Component({
  selector: 'page-tab-set1',
  templateUrl: 'tab-set1.html'
})
export class TabSet1Page {

  tab1Root: any = HomePage;
  tab2Root: any = AboutPage;

  constructor() { }

}

.html

<ion-tabs>
  <ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab>
  <ion-tab [root]="tab2Root" tabTitle="About" tabIcon="information-circle"></ion-tab>
</ion-tabs>

第 2 步:

app.component.ts

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = TabsPage;

  constructor(public events: Events) {

    this.subscribePendingRequests();//subscribe
    this.events.publish('pending-requests', 0);//publish
  }

  //subscribe
  subscribePendingRequests() {
    this.events.subscribe('pending-requests', (data) => {
      if (data > 0) {
        this.rootPage = TabSet1Page;
      }
      else {
        this.rootPage = TabsPage;
      }
    });
  }

}

第 3 步:

about.ts

 @Component({
      selector: 'page-about',
      templateUrl: 'about.html'
    })
    export class AboutPage {

      constructor(public events: Events) { }

      //refresh
      refreshPendingRequests() {
        this.events.publish('pending-requests', 5);
      }
   } 

about.html

<ion-header>
  <ion-navbar>
    <ion-title>
      About
    </ion-title>
     <ion-buttons end>
      <button ion-button icon-only (click)="refreshPendingRequests()">
        <ion-icon name="ios-funnel"></ion-icon>
      </button>
    </ion-buttons>
  </ion-navbar>
</ion-header>

【讨论】:

  • 谢谢,根据我对您的代码的理解,想法是有两个不同的页面,其中一个有 2 个选项卡,另一个有 3 个页面,然后根据值更改根页面,是否正确?跨度>
  • 对此有何反馈?
  • 我知道这会起作用,但我有两个选项卡要显示隐藏然后我必须创建 4 个组合来支持它
  • 这是您唯一的选择。由于标签设计的性质,您不能像其他 HTML 元素那样执行此操作。因为标签就像根页面一样工作。所以如果您需要继续这个用例,你必须使用上面的解决方案。
猜你喜欢
  • 2017-04-03
  • 1970-01-01
  • 2022-08-11
  • 2016-12-22
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多