离子2如何在标签中增加徽章

Ahm*_*ory 7 ionic2 angular

如果收到通知,我会尝试更改徽章的价值,但如果您有任何想法可以帮助我,我不会来交换.

tabs.ts

import { Component, ViewChild } from '@angular/core';

import { SQLite, Dialogs, Firebase } from 'ionic-native';
import { NavController, Platform, Tabs } from 'ionic-angular';
import { HomePage } from '../home/home';
import { NotificationPage } from '../notification/notification';
import { MessagesPage } from '../messages/messages';
import { MoiPage } from '../moi/moi';

import {GlobalVars} from '../../providers/varglobal'
import {Injectable} from '@angular/core';

@Injectable()

@Component({
  templateUrl: 'tabs.html'
})

export class TabsPage {
  // this tells the tabs componnent which Pages
  // should be each tab's root Page
 @ViewChild('myTabs') tabRef: Tabs;
    public database: SQLite;
    public people: Array<Object>;
    public home:any='';


  tab1Root: any = HomePage;
  tab3Root: any = NotificationPage;
  tab4Root: any = MessagesPage;
  tab5Root: any=MoiPage;

  constructor(private platform:Platform,private nav:NavController,public lan: GlobalVars) {
this.platform.ready().then(() => {
              Firebase.onNotificationOpen()
  .subscribe(res => {
    if(res.tap) {
      // background mode
      console.log(res.tap);
      console.log(res);


    } else if (!res.tap) {
      // foreground mode



    let num=parseInt(this.lan.getbadgeHome());
     num=num+1;
     alert(num+' home');
     this.home=num;  
     alert(this.home);  





    }
  });

        });




}
Run Code Online (Sandbox Code Playgroud)

tabs.html

<ion-tabs  color="primary">


   <ion-tab [root]="tab1Root"  tabIcon="home" tabBadge={{"home"}} tabBadgeStyle="danger"></ion-tab>
  <ion-tab [root]="tab4Root"  tabIcon="mail" tabBadgeStyle="danger"></ion-tab>
  <ion-tab [root]="tab5Root"  tabIcon="person" ></ion-tab>

</ion-tabs>
Run Code Online (Sandbox Code Playgroud)

事实上,我设法检索通知并增加变量num并将其放在变量home中,但徽章的值不会改变

Phi*_*tin 6

根据您的标签示例,这应该有效:

<ion-tab [root]="tab1Root"  tabIcon="home" tabBadge="{{tab1BadgeCount}}" tabBadgeStyle="danger"></ion-tab>
Run Code Online (Sandbox Code Playgroud)

使用简单的控制器:

export class TabsPage  {

  tab1Root: any = SomePage;
  tab1BadgeCount : number = 0; // default 0

  constructor() {

  }

  incrementBadgeCount(){
      this.tab1BadgeCount = this.tab1BadgeCount+1;
  }

  decrementBadgeCount(){
    this.tab1BadgeCount = this.tab1BadgeCount-1;
  }

}
Run Code Online (Sandbox Code Playgroud)

您可以将这些方法挂钩到一个按钮进行测试.

但是,我建议使用事件来控制徽章数量的更改,例如

export class TabsPage  {

  tab1Root: any = SomePage;
  tab1BadgeCount : number = 0; // default 0

  constructor(public events: Events) {

  }

  incrementBadgeCount(){
      this.tab1BadgeCount = this.tab1BadgeCount+1;
      this.publishBadgeCountUpdate();
  }

  decrementBadgeCount(){
    this.tab1BadgeCount = this.tab1BadgeCount-1;
    this.publishBadgeCountUpdate();
  }

}


  subscribeToBadgeCountChange(){
      // Method to run when tab count changes
      return this
      .events
      .subscribe("tabs-page:badge-update", this.refreshBadgeCount());
  }

  publishBadgeCountUpdate(){
      // Call this method when you have changed the count
    return this
        .events
        .publish("tabs-page:badge-update");
  }

  refreshBadgeCount(){
    // This method will be called when incrementBadgeCount or decrementBadgeCount are executed.
    // The beauty of the events approach is that you can cause a change to the tab count from any other view, without referencing the tabs page directly.

  }

  ionViewWillEnter() {
    this.subscribeToBadgeCountChange();
  }
}
Run Code Online (Sandbox Code Playgroud)


Ara*_*ind 4

正如您在文档 中看到的那样,tabBadge不是输入属性,而是一个属性。所以应该使用如下

<ion-tab [root]="tab1Root"  tabIcon="home" tabBadge="{{home}}" tabBadgeStyle="danger"></ion-tab>
Run Code Online (Sandbox Code Playgroud)

更新 :

由于您在构造函数中分配值,因此在更改时您将无法获取更新的值。我需要更多信息来解决这个问题。一种替代方法是使用超时功能,以便每 60 秒检查一次最新值,如下所示

setInterval(function() {
    this.platform.ready().then(() => {
              Firebase.onNotificationOpen()
  .subscribe(res => {
    if(res.tap) {
      // background mode
      console.log(res.tap);
      console.log(res);


    } else if (!res.tap) {
      // foreground mode



    let num=parseInt(this.lan.getbadgeHome());
     num=num+1;
     alert(num+' home');
     this.home=num;  
     alert(this.home);  





    }
  });
}, 60 * 1000);
Run Code Online (Sandbox Code Playgroud)

这将订阅该服务并更新您的通知计数。