我正在尝试制作一个从标签页打开的通知页面。打开页面后,它需要自行刷新,以便显示新通知,并且这些通知将被检查为已读,并且选项卡上的徽章将为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);
});
}
标签页;
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);
});
}
标签视图;
答案 0 :(得分:4)
问题是页面是第一次创建的,然后因为它已经存在,所以不再创建它。如果每次选择选项卡时都需要执行一些代码,请使用ionViewDidEnter
或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 :(得分:2)
你可以像Ionic一样使用Lifecycle Hooks:
ionViewWillEnter {
// do something when page is about to enter
}
ionViewDidEnter() {
// do something when page has entered
}