2017-08-02 2 views
2

J'essaie de créer une page de notification qui s'ouvre à partir des onglets. Après avoir ouvert la page, elle doit se rafraîchir pour que les nouvelles notifications soient visibles et que ces notifications soient lues et que le badge sur les onglets soit 0.Les onglets ioniques n'entrent pas dans le constructeur de la page plusieurs fois

La première fois que vous ouvrez la page de notification, il entre dans le constructeur mais il ne rentre pas.

Ça fonctionnait bien avant mais ça ne marche plus, je ne sais pas pourquoi.

Page de notification;

constructor(public events: Events, public navCtrl: NavController, public 
    navParams: NavParams, private notificationservice: NotificationService, 
    private loadingCtrl: LoadingController) { 

this.notificationservice.getNotifications(AuthService.currentUser.UserId).then(
data => { 
    this.notifications = data; 

    this.notificationservice.unreadNotificationCount().then(data => { 
    if(data != 0) 
    { 
     this.notificationservice.readNotification().then(data => { 
     this.updateBadge(); 
     this.navCtrl.setRoot(this.navCtrl.getActive().component); 
     }); 
    } 
    }); 
}); 
} 

public updateBadge() 
{ 
    this.notificationservice.unreadNotificationCount().then(data => { 
    console.log(data); 
    this.events.publish('cart:updated', data); 
    }); 
} 

Onglets Page;

constructor(public navCtrl: NavController, public navParams: NavParams, 
private notificationservice: NotificationService, public events: Events) { 

    this.updateBadge(); 

    this.events.subscribe('cart:updated', (count) => {  
    this.badge = count; 
    }); 
} 

public updateBadge() 
{ 
    this.notificationservice.unreadNotificationCount().then(data => { 
    console.log(data); 
    this.events.publish('cart:updated', data); 
    }); 
} 

Affichage des onglets;

Tabs with badge

+1

[. S'il vous plaît ne pas poster votre code comme une image] (// meta.stackoverflow.com/q/285551) –

+1

@suraj Je l'édite. – esdoodle

Répondre

1

La chose est que la page est créée que la première fois, et étant donné qu'il existe déjà, il est pas créé de nouveau . Si vous avez besoin d'exécuter un code à chaque fois qu'un onglet est sélectionné, utilisez les ionViewDidEnter ou les crochets du cycle de vie ionViewWillEnter:

constructor(...) { 
    // Will be executed only once 
    // ... 
} 

ionViewWillEnter() { 
    // Will be executed every time the user selects this tab 
    // ... 
} 

// or 

ionViewDidEnter() { 
    // Will be executed every time the user selects this tab 
    // ... 
} 
1

Vous pouvez utiliser Lifecycle Hooks dans ionique comme ceci:

ionViewWillEnter { 
    // do something when page is about to enter 
} 

ionViewDidEnter() { 
    // do something when page has entered 
}