2017-08-02 19 views
2

我试图使通知页面从选项卡打开。打开页面后,需要自行刷新,以便新通知可以看到,并且这些通知将被检查为已读,并且标签上的徽章将为0.离子选项卡不要多次输入页面的构造函数

第一次打开通知页面时,它进入构造函数但它不会再次进入。

之前运行良好,但之后停止工作,我不知道为什么。

通知页;

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); 
    }); 
} 

Tabs 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); 
    }); 
} 

Tabs view;

Tabs with badge

+1

[请不要发布您的代码为图像(// meta.stackoverflow.com/q/285551) –

+1

@suraj我在编辑它。 – esdoodle

回答

1

的事情是,该页面被创建只有第一次,然后因为它已经存在,它不会再创建。如果您需要在每次选择一个选项卡的时间来执行一些代码,使用ionViewDidEnterionViewWillEnter生命周期挂钩:

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

可以在离子使用Lifecycle Hooks像这样:

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

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