我注意到每次我在堆栈中推送页面时都会调用ionViewloaded
并添加subscribe()
我的问题是当我弹出页面时我无法取消订阅。这是我的代码。它不起作用。
ionViewDidLoad() {
this.user = this.navParams.data;
this.events.subscribe('close:userPopOver', this.onRemovePopOver.bind(this));
}
ionViewWillLeave() {
alert('FIRES!!');
this.events.unsubscribe('close:userPopOver', this.onRemovePopOver.bind(this));
}
答案 0 :(得分:2)
两个答案都无法实现,因为打字稿没有识别传递给订阅实例的void值,使其工作
this.events.unsubscribe('close:userPopOver',null)
https://forum.ionicframework.com/t/events-pub-sub-how-to-unsubscribe-in-lifecycle-hook/50596/5
答案 1 :(得分:0)
private sub : Subscription;
ionViewDidLoad() {
this.user = this.navParams.data;
this.sub = this.events.subscribe('close:userPopOver', this.onRemovePopOver.bind(this) );
}
ionViewWillLeave(){
alert('FIRES!!');
this.sub.unsubscribe();
}