好吧,我有一个侧菜单应用程序,其中第一页是主页,一旦用户进入应用程序,它就会加载元素。我正在使用 Ionic 2,并且正在通过套接字进行通信。一个细节是,当我更新时元素不会加载,但是当我单击菜单按钮并重新加载元素时,有人可以帮助我吗?
遵循组件代码:
export class HomePage {
eventsList: Event [];
constructor (
public navCtrl: NavController,
public eventService: EventService,
public platform: Platform){}
ionViewDidLoad () {
console.info ('HomePage initializated');
this.platform.ready().then(() => {
this.eventService
.getAllEvents()
.subscribe(
events => {
this.eventsList = events;
console.log(events);
},
err => console.log("ERROR!", err))
});
}
}
当您推送新页面时,您需要取消订阅订阅,否则您将有内存泄漏。因此,请始终取消订阅。
在下面的代码中,当页面被销毁时,我取消订阅。
export class HomePage implements OnDestroy {
eventsList: Event[];
destroy$ : Subject<any> = new Subject();
constructor(
public navCtrl: NavController,
public eventService: EventService,
public platform: Platform){}
ionViewDidLoad () {
console.info ('HomePage initializated');
this.platform.ready().then(() => {
this.eventService
.takeUntil(this.destroy$)
.getAllEvents()
.subscribe(
events => {
this.eventsList = events;
console.log(events);
},
err => console.log("ERROR!", err))
});
}
ngOnDestroy(){
this.destroy$.next();
this.destroy$.unsubscribe();
}
}